How to briefly assign and immediately call a function variable?

The following is a method for defining an anonymous function in closure, calling a function, and forgetting it:

(function () { "do stuff"; })(); 

This is used to support a limited area without adding a lot of volume to the script (IIFE: Immediately-Invoked Function Expression expression).

What to do if you hope to immediately execute the function while maintaining the function for future use, for example:

 var doThing; (doThing = function () { "do stuff"; })(); 

This works in the browsers I tested (Chrome, FF, IE8, IE10), but it does not miss JSLint (Bad Invocation). Are there compatibility issues with this method?

Is there a way to accomplish this that JSLint kindly looks at?

+5
javascript function jslint iife
Sep 10 '12 at 20:27
source share
5 answers

If jslint transfer is absolutely necessary, then:

 var doThing; (doThing = function () { "do stuff"; }).call(); 

must do the job.

Edit

To pass parameters during .call

 var doThing; (doThing = function (param1, param2) { 'do stuff'; }).call(this, arg1, arg2); 
+7
Sep 10
source share

You should not assign a link to a function and call the function at the same time. This can cause the heads to explode, as this adds to the already existing possibilities of assigning a variable to a link, assigning a variable to a return, or assigning a variable to the result of the assignment. A way to make it possible for anyone else to read your code would be to do this in two lines. If you want to wrap this in a function, it would be something like this:

 var doThing = (function() { var inner = function() { //doThing body }; inner(); return inner; })(); 

This in itself buys you nothing but obfuscation, doing it in a simple way, since the call will not be held independently. However, you could create a function that does this:

 var callAndRef = function(funk) { funk(); return funk; }; 

and then

 var doThing = callAndRef(function() { //doThing body }); 

or if you felt even more amiable and worked with you JS guru, you can drop it on Function.prototype and associate it with the declaration. You can also perforate in call / apply to support the this link and arguments (but it looks like it will be just noise for your current question).

All this must be done with utmost care . Of course, you should not go this route to save yourself, to type a couple of additional lines on one command.

+3
Sep 10 '12 at 20:32
source share

A little reordering of the code will help.

 var doThing = function () { "do stuff"; }; doThing(); 

or

 var doThing = function () { "do stuff"; }; (doThing)(); 
+3
Sep 10
source share

If you want to fix the Bad Invocation error, you can do this:

 var doThing; doThing = function () { "do stuff"; }; doThing(); 

By separating the declaration and assignment, you will make your code more understandable and maintainable. Another option is to declare a function with the name:

 function doThing() { "do stuff"; } doThing(); 
0
Sep 10 '12 at 21:15
source share

JSLink - Crawford's personal opinion on how to write Javascript. Most of them are great advice, but sometimes his opinion seems to get in the way.

This is a more standard way to write this line:

 var doThing = (function () { "do stuff"; }()); 

Note that the whole (function ...) is inside the paren.

-one
Sep 10
source share



All Articles