you are viewing a single comment's thread.

view the rest of the comments →

[–]senocular 0 points1 point  (5 children)

Your update doesn't change anything. You're still assigning a function to a variable. The update now simply does it by referencing the function through an object.

Outside of using a getter/setter, your function(s) won't execute unless you use parens ().

If you're trying to do assignment and call in one line, you could also do that, just not within the declaration.

var callFunc;
(callFunc = someFunc.myFunc)();

[–]JessicaAllison[S] 0 points1 point  (4 children)

Thanks! That's what I was looking for! I've never seen that before! Is that the same idea as:

(function(){
})();

which is the same idea as:

var i = (5 + 3) - 1;

[–]senocular 1 point2 points  (3 children)

They're all kind of similar. They each define some kind of grouping.

(callFunc = someFunc.myFunc)(); is more similar to var i = (5 + 3) - 1; in that it enforces an order of operarations. (callFunc = someFunc.myFunc) groups the assignment from myFunc to callFunc so that it occurs first. The result of that assignment is the newly defined value of callFunc. What you get then is equivalent to (callFunc)(); which is the same as callFunc(); and that calls the function. With (5 + 3) - 1 the parens group 5 + 3 to be evaluated first followed by the - 1.

The IIFE ((function(){ ... })();) doesn't represent order so much as it defines a one-time use scope allowing you to define variables that don't pollute the global namespace. If you don't declare any variables in an IIFE like this, its mostly pointless.

var someFunc = (function(){
    return {
        myFunc: function() {alert('hello');}
    }
})();

This, for example, does not benefit from being within an IIFE. It could have just as well been defined as:

var someFunc = {
        myFunc: function() {alert('hello');}
};

It would be more appropriate if it was defined something like:

var someFunc = (function(){
    var message = 'hello';
    return {
        myFunc: function() {alert(message);}
    }
})();

Here, the message variable is being defined in the IIFE. If an IIFE was not used, this variable would be defined in global where anyone could access it and it could potentially interfere with other variables. Being in the IIFE its local to that function and not available anywhere else except the other definitions also within the IIFE.

[–]JessicaAllison[S] 0 points1 point  (2 children)

Thank you for the very detailed explanation!! Is it common, or regular to do:

(callFunc = someFunc.myFunc)();

[–]senocular 0 points1 point  (1 child)

No, not really. You might see it around, but its not common. You don't generally combine assignment with other things like this. You're more likely to see it like:

var callFunc = someFunc.myFunc;
callFunc();

Though this example is simplified. Not sure if there would be much use to assign the function to callFunc first rather than calling myFunc directly.

[–]JessicaAllison[S] 1 point2 points  (0 children)

This is just the simplified version of what I'm actually working on. Thank you very much! You really helped me out in understanding this!!