From: Jesse on
Hey, I've got a question about self invoking functions in javascript.

What I'm doing is something similar to the following

myNamespace = {}; //namespace for holding any objects/functions

//helpModule as an example
myNamespace.HelpModule = new (function(){
this.abc = '123';
//lots of other code in here...
return this;
})();

However jsLint doesn't like that saying "Weird construction. Delete
'new'.".

But it seems to work fine, and the reason I'm using it is to have
"this" scope to the function instead of the window. I could just
define it as an object literal or do something similar to

myNamespace.HelpModule = (function(){
var obj = {};
obj.abc = '123';

return obj;
}();

but for some reason I like having the 'this' keyword
From: David Mark on
On Jul 29, 8:39 pm, Jesse <imje...(a)gmail.com> wrote:
> Hey, I've got a question about self invoking functions in javascript.
>
> What I'm doing is something similar to the following
>
> myNamespace =  {}; //namespace for holding any objects/functions

It's just an object and your code would be clearer without the
comment.

>
> //helpModule as an example
> myNamespace.HelpModule = new (function(){
>     this.abc = '123';
>     //lots of other code in here...
>     return this;
>
> })();

Lose the - new - keyword, refactor.

>
> However jsLint doesn't like that saying "Weird construction. Delete
> 'new'.".

I heartily agree with Douglas on that one. :)

>
> But it seems to work fine, and the reason I'm using it is to have
> "this" scope to the function instead of the window.

Not the window, the Global Object.

> I could just
> define it as an object literal or do something similar to
>
> myNamespace.HelpModule = (function(){
>     var obj = {};
>     obj.abc = '123';
>
>     return obj;
>
> }();
>
> but for some reason I like having the 'this' keyword

Why? It's a very awkward construct.
From: RobG on
On Jul 30, 10:39 am, Jesse <imje...(a)gmail.com> wrote:
> Hey, I've got a question about self invoking functions in javascript.
>
> What I'm doing is something similar to the following
>
> myNamespace = {}; //namespace for holding any objects/functions
>
> //helpModule as an example
> myNamespace.HelpModule = new (function(){
> this.abc = '123';
> //lots of other code in here...
> return this;
> })();

All that seems to do is put an extra (useless) object on the returned
object's internal prototype chain. There is no need for the return
statement, functions called with - new - return their this object by
default. A weird construct indeed.


> However jsLint doesn't like that saying "Weird construction. Delete
> 'new'.".
>
> But it seems to work fine, and the reason I'm using it is to have
> "this" scope to the function instead of the window. I could just

A function's this keyword has nothing to do with scope, it is a
reference to an object. It is set by the call to the function, you
can't control it by how you create the function other than by limiting
how it is called.

> define it as an object literal

Do that - hey look, less code!

var myNamespace = {
HelpModule: {
abc: '123'
}
};


> or do something similar to
>
> myNamespace.HelpModule = (function(){
> var obj = {};
> obj.abc = '123';
>
> return obj;
> }();

Which is essentially wrapping an object literal in a function that
just returns the object and possibly creates useless closures. If you
want to use closures for a specific purpose, use the module pattern
(see below). Otherwise why call a function when an object literal will
do the job (and probably do it faster)?

var myNamespace = (function() {

var closedVar_1 = '123';

return {
helpModule: {
getVar_1: function() {
return closedVar_1;
},

setVar_1: function(value) {
closedVar_1 = value;
return closedVar_1;
}
}
}
})();


You may want to read the following blog entry by Peter Michaux:

<URL: http://peter.michaux.ca/articles/javascript-namespacing >


> but for some reason I like having the 'this' keyword

So it's a fashion statement? ;-)

I think Peter gives some good reasons for avoiding the this keyword
with namespaces (in short, because you can't guarantee how a function
will be called so you can't be sure you know what its this keyword
will reference) and provides some alternative strategies.


--
Rob
From: David Mark on
On Jul 29, 11:38 pm, RobG <rg...(a)iinet.net.au> wrote:
> On Jul 30, 10:39 am, Jesse <imje...(a)gmail.com> wrote:
>
> > Hey, I've got a question about self invoking functions in javascript.
>
> > What I'm doing is something similar to the following
>
> > myNamespace =  {}; //namespace for holding any objects/functions
>
> > //helpModule as an example
> > myNamespace.HelpModule = new (function(){
> >     this.abc = '123';
> >     //lots of other code in here...
> >     return this;
> > })();
>
> All that seems to do is put an extra (useless) object on the returned
> object's internal prototype chain. There is no need for the return
> statement, functions called with - new - return their this object by
> default. A weird construct indeed.
>
> > However jsLint doesn't like that saying "Weird construction. Delete
> > 'new'.".
>
> > But it seems to work fine, and the reason I'm using it is to have
> > "this" scope to the function instead of the window. I could just
>
> A function's this keyword has nothing to do with scope, it is a
> reference to an object. It is set by the call to the function, you
> can't control it by how you create the function other than by limiting
> how it is called.

Thanks for picking that up. Somehow I missed that offending use of
the term. It seems to be so pervasive that virtually every developer
who uses the "major" libraries thinks that scope is the - this -
object; which leaves the question of what they would call the concept
of scope (if they are even aware of the concept).

>
> > define it as an object literal
>
> Do that - hey look, less code!
>
> var myNamespace = {
>   HelpModule: {
>     abc: '123'
>   }
>
> };

That's ideal, but I think they were implying that there would be lots
more inside the one-off function.

> > or do something similar to
>
> > myNamespace.HelpModule = (function(){
> >     var obj = {};
> >     obj.abc = '123';
>
> >     return obj;
> > }();
>
> Which is essentially wrapping an object literal in a function that
> just returns the object and possibly creates useless closures. If you
> want to use closures for a specific purpose, use the module pattern
> (see below). Otherwise why call a function when an object literal will
> do the job (and probably do it faster)?
>
> var myNamespace = (function() {
>
>   var closedVar_1 = '123';
>
>   return {
>     helpModule: {
>       getVar_1: function() {
>         return closedVar_1;
>       },
>
>       setVar_1: function(value) {
>         closedVar_1 = value;
>         return closedVar_1;
>       }
>     }
>   }
>
> })();
>
> You may want to read the following blog entry by Peter Michaux:
>
> <URL:http://peter.michaux.ca/articles/javascript-namespacing>
>
> > but for some reason I like having the 'this' keyword
>
> So it's a fashion statement? ;-)

This is the new that. :)

>
> I think Peter gives some good reasons for avoiding the this keyword
> with namespaces (in short, because you can't guarantee how a function
> will be called so you can't be sure you know what its this keyword
> will reference) and provides some alternative strategies.
>

Yes, if a "namespace" object is simply a container for various
functions (to keep the global scope clean), then there is no reason to
rewrite the functions as methods.

This helps with both performance and minification as well as you can
set local references to the functions in the calling code. Those are
two reasons why I went this way with My Library.

I went a long way toward converting Dojo (in my branch) to this
strategy as well. Unfortunately, they decided to un-declare their
global variables instead (ostensibly to make their pervasive lookups
faster). I tried to tell them. :)

Most of the "majors" go all out the other way and constantly reference
their own "namespace" inside of even the most basic functions, which
slows things down and impedes minification.
From: John G Harris on
On Thu, 29 Jul 2010 at 17:46:07, in comp.lang.javascript, David Mark
wrote:
>On Jul 29, 8:39�pm, Jesse <imje...(a)gmail.com> wrote:
>> Hey, I've got a question about self invoking functions in javascript.
>>
>> What I'm doing is something similar to the following
>>
>> myNamespace = �{}; //namespace for holding any objects/functions
>
>It's just an object and your code would be clearer without the
>comment.
<snip>

It's an object used to implement a namespace (by hand, as is common in
javascript).

There *should* be a short comment. It should say briefly what this
global variable is for, to be read by maintenance programmers in two
year's time.

What's wrong is the name. When Jesse's code and Jim's code is merged
next year then Jesse's myNameSpace and Jim's myNameSpace will clash
horribly. Also it's far too long to be written many, many times.

John
--
John Harris