PDA

View Full Version : extra () on the last line of EventManager.js



sjivan
12 Nov 2006, 1:13 PM
There appears to be an extra () on the last line of EventManager.js

}();

jack.slocum
12 Nov 2006, 7:05 PM
That's on purpose. It executes it immediately to make it a single instance object.

sjivan
27 Nov 2006, 10:28 PM
I thought you could do



YAHOO.ext.EventManager = new function(){..};


or


YAHOO.ext.EventManager = function(){..}();


to get a new singleton instance, but the code has the 'new' operator and (). Just curious if both are required here ..

Thanks,
Sanjiv

jack.slocum
28 Nov 2006, 5:14 AM
They are synonymous. It's the same as:


function Foo(){

}

var f = new Foo;
var f2 = new Foo();

Both work, but I always use parenthesis.

I could be wrong though.

sjivan
28 Nov 2006, 5:02 PM
You presently have

YAHOO.ext.EventManager = new function(){..} ();

My question was do you need both, new and the trailing ();?

Maybe I'm misunderstanding the intent. Are you creating a singleton instance 'YAHOO.ext.EventManager' ?

If so, then then wouldnt using either "new" or the trailing (); suffice to result in instantiating the function?

ie :



YAHOO.ext.EventManager = new function(){..};


or


YAHOO.ext.EventManager = function(){..}();


I stumbled upon this article http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/

which states


It is never a good idea to put new directly in front of function. For example, new function provides no advantage in constructing new objects.

myObj = new function () {
this.type = 'core';
};


and



If we are making an object containing methods that are bound to private variables and functions, it is still better to leave off the new prefix.

var foo = new function() {
function processMessages(message) {
alert("Message: " + message.content);
}
this.init = function() {
subscribe("/mytopic", this, processMessages);
}
}

By using new to invoke the function, the object holds onto a worthless prototype object. That wastes memory with no offsetting advantage. If we do not use the new, we don’t keep the wasted prototype object in the chain. So instead we will invoke the factory function the right way, using ().

var foo = function () {
function processMessages(message) {
alert("Message: " + message.content);
}
return {
init: function () {
subscribe("/mytopic", this, processMessages);
}
};
}();




So it apprears that using a trailing (); is prefered over using the new operator when creating singletons.. but back to my question regarding the yui-ext YAHOO.ext.EventManager code, do we need both, the new operator and the trailing (); for function instantation?

Sanjiv

sjivan
28 Nov 2006, 5:07 PM
its actually on YAHOO.ext.EventObject and not YAHOO.ext.EventManager.

jack.slocum
28 Nov 2006, 5:30 PM
Did you look at the code I posted? Here I will edit it:


function EventManager(){

}

var f = new EventManager;
var f2 = new EventManager();

Both have the same result. Replacing the named function with an anonymous (as it is defined now) doesn't change anything. Is the () required? no. What happens if you don't include it? It gets called implicitly. I prefer to include the extra 2 characters so it's obvious what's going on.

There is a difference between that code in that article and the EventManager code. You can't just remove the () and all is well. It would have to be reworked into a return {} statement. Is that better for singletons? That is debatable IMO. He is comparing a built in JS feature (new+function) (EventManager way) vs return {} (function call + new object). Which is better for a singleton? I think that depends on the structure of the class. If you have alot of private vars, and your private functions do not access public functions, return {} is the way to go. I'm sure you see me using it plenty. But for EventManager which has back and forth calls, it makes sense to use the standard construct.

jack.slocum
28 Nov 2006, 5:36 PM
BTW, if you suggesting removing the "new" and just using (), "this" would point to the window object.

sjivan
28 Nov 2006, 8:33 PM
Okay, I finally got it in my head. Thanks for your patience.

Sanjiv