1. #1
    Ext User
    Join Date
    Mar 2007
    Location
    Boston
    Posts
    349
    Vote Rating
    0
    sjivan is on a distinguished road

      0  

    Default extra () on the last line of EventManager.js

    extra () on the last line of EventManager.js


    There appears to be an extra () on the last line of EventManager.js

    }();

  2. #2
    Sencha User jack.slocum's Avatar
    Join Date
    Mar 2007
    Location
    Tampa, FL
    Posts
    6,955
    Vote Rating
    16
    jack.slocum will become famous soon enough

      0  

    Default


    That's on purpose. It executes it immediately to make it a single instance object.

  3. #3
    Ext User
    Join Date
    Mar 2007
    Location
    Boston
    Posts
    349
    Vote Rating
    0
    sjivan is on a distinguished road

      0  

    Default


    I thought you could do

    Code:
    YAHOO.ext.EventManager = new function(){..};
    or
    Code:
    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

  4. #4
    Sencha User jack.slocum's Avatar
    Join Date
    Mar 2007
    Location
    Tampa, FL
    Posts
    6,955
    Vote Rating
    16
    jack.slocum will become famous soon enough

      0  

    Default


    They are synonymous. It's the same as:

    Code:
    function Foo(){
    
    }
    
    var f = new Foo;
    var f2 = new Foo();
    Both work, but I always use parenthesis.

    I could be wrong though.

  5. #5
    Ext User
    Join Date
    Mar 2007
    Location
    Boston
    Posts
    349
    Vote Rating
    0
    sjivan is on a distinguished road

      0  

    Default


    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 :

    Code:
    YAHOO.ext.EventManager = new function(){..};
    or
    Code:
    YAHOO.ext.EventManager = function(){..}();
    I stumbled upon this article http://yuiblog.com/blog/2006/11/13/j...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

  6. #6
    Ext User
    Join Date
    Mar 2007
    Location
    Boston
    Posts
    349
    Vote Rating
    0
    sjivan is on a distinguished road

      0  

    Default


    its actually on YAHOO.ext.EventObject and not YAHOO.ext.EventManager.

  7. #7
    Sencha User jack.slocum's Avatar
    Join Date
    Mar 2007
    Location
    Tampa, FL
    Posts
    6,955
    Vote Rating
    16
    jack.slocum will become famous soon enough

      0  

    Default


    Did you look at the code I posted? Here I will edit it:

    Code:
    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.

  8. #8
    Sencha User jack.slocum's Avatar
    Join Date
    Mar 2007
    Location
    Tampa, FL
    Posts
    6,955
    Vote Rating
    16
    jack.slocum will become famous soon enough

      0  

    Default


    BTW, if you suggesting removing the "new" and just using (), "this" would point to the window object.

  9. #9
    Ext User
    Join Date
    Mar 2007
    Location
    Boston
    Posts
    349
    Vote Rating
    0
    sjivan is on a distinguished road

      0  

    Default


    Okay, I finally got it in my head. Thanks for your patience.

    Sanjiv

Similar Threads

  1. do u have extra comma? ok, ie will destroy your application!
    By luke83 in forum Ext 1.x: Help & Discussion
    Replies: 40
    Last Post: 17 Sep 2008, 12:36 AM
  2. Content panel tab line up
    By bidyut in forum Ext 1.x: Help & Discussion
    Replies: 5
    Last Post: 20 Jun 2007, 11:38 AM
  3. Ext.form.ComboBox + Ext.data.HttpProxy + extra params
    By Saeven in forum Ext 2.x: Help & Discussion
    Replies: 9
    Last Post: 21 Mar 2007, 12:41 AM
  4. IE doesn't like line 236 of DomHelper
    By kjordan in forum Ext 2.x: Help & Discussion
    Replies: 8
    Last Post: 18 Mar 2007, 1:36 PM
  5. extra 1px space in dialog handle style
    By VinylFox in forum Ext 2.x: Help & Discussion
    Replies: 2
    Last Post: 3 Mar 2007, 3:23 PM

Thread Participants: 1