Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I do that a bit, but found it tricky to end everything with a comma, and effectively use ":" instead of "=" for declarations. Lately I've been doing

    projectname = (function(){
        self = {};

        self.A_CONSTANT = ...;

        self.someFunc = function(...){
        };

        self.lastFunc = function(...){
        };

        return self;
    })();
I think that produces the exact same end result, but someone set me straight if I'm mistaken.


What you do is a bit more complicated, but has the benefit of making your data "private" within a closure. You are using a function as an object, and assigning other functions as fields within its "prototype". I think these inner functions can call each other w/out a prefix, as methods of the same object.

My version is the "static method" version that requires the namespace and a dot before any function calls. I'm OK with that, unless I need a bunch of instances of something.

P.S. - the semicolons don't really do anything (other than make Doug C feel good), as JS is a line oriented language, and will complete statements when they "look done", semis or not. Think of semicolons in JS like colons in BASIC: they let you put a second statement on the same line.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: