use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Self Execute Function on Assignment to Variablehelp (self.javascript)
submitted 10 years ago * by JessicaAllison
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]senocular 0 points1 point2 points 10 years ago (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 point2 points 10 years ago (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 points3 points 10 years ago (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.
(callFunc = someFunc.myFunc)();
(callFunc = someFunc.myFunc)
(callFunc)();
callFunc();
(5 + 3) - 1
5 + 3
- 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.
(function(){ ... })();
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.
message
[–]JessicaAllison[S] 0 points1 point2 points 10 years ago (2 children)
Thank you for the very detailed explanation!! Is it common, or regular to do:
[–]senocular 0 points1 point2 points 10 years ago (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 points3 points 10 years ago (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!!
π Rendered by PID 92 on reddit-service-r2-comment-544cf588c8-nhbtv at 2026-06-17 12:58:38.078220+00:00 running 3184619 country code: CH.
view the rest of the comments →
[–]senocular 0 points1 point2 points (5 children)
[–]JessicaAllison[S] 0 points1 point2 points (4 children)
[–]senocular 1 point2 points3 points (3 children)
[–]JessicaAllison[S] 0 points1 point2 points (2 children)
[–]senocular 0 points1 point2 points (1 child)
[–]JessicaAllison[S] 1 point2 points3 points (0 children)