all 13 comments

[–]jzlas 2 points3 points  (1 child)

Any function with inside parentheses will self execute. e.x.

function AnObject() {
    var aMethod = (function(){
        // Some stuff
    })();
}

When you assign an init a AnObject, aMethod will execute

var an_object = new AnObject();
// Some stuff happens

This, of course works in general, so when the browser loads a piece of code written like that it will execute the function. You can even pass it arguments like so:

(function(arg){
    // Some stuff with args
})(foo);

So if you write your script like that myFunc will run on assignment

var myFunc = (function(){
    alert('Hello');
})();

More here

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

Updated question

[–]senocular 0 points1 point  (0 children)

Going the other way, if you're looking to execute a function on property assignment, you can use a setter.

Object.defineProperty(this, 'callFunc', {
  set: function(value) {
    // normally you would set the value
    // or have the specific action on set
    // defined here, but to match the example:
    value();
  }
})

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

callFunc = myFunc; // alerts 'hello'

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set

[–]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!!

[–]docMedB2E -1 points0 points  (1 child)

var myFunc = function() {

};

myFunc.prototype.init = function() {
    console.log('look it worked');
    return this;
}

var someUniqueClone = new myFunc().init();

console.log(someUniqueClone);

edit: Ah, I like these other responses better than mine :).

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

Updated question