Javascript self-run function "not a function"

I have:

var Init = (function() { my js goes here })(); 

And my js execute correctly when the page loads. I also have:

 $('form :checkbox').change(function() { Init(); }); 

But firebug says Init is not a function.

+21
javascript iife
May 22 '11 at 21:30
source share
6 answers

This is not a function.

 (function() { ... })() 

automatically evaluates anonymous function. And the result of the assessment, apparently, does not return the functional object in this case :-)

Consider:

 f = (function() { return "not a function :(" })() alert(f()) 

and

 f = (function() { return function () { return "Yay!" } })() alert(f()) 

Happy coding :)




Here is a function that will "execute something once" and then "return something to execute later." (See "You can either [assign] a function or call it, you cannot execute both ..." from Slaks answer.) However, I would not do it like that.

 Init = (function () { function Init () { alert("whee!") } Init() return Init })() Init() 

Here is another solution (much shorter / cleaner) from CD Sanchez (see comment) that uses the fact that assignment is evaluated by the assigned value:

 var Init; (Init = function Init () { alert ("wee"); })() 
+54
May 22 '11 at 21:33
source share

Init not a function; this is the result of calling a function.

You can either create a function or call it; you cannot do both at once.

Technically, you can fix this by adding return arguments.callee; to return the function from the call.
However, this is a stupid idea.

You should probably not call a function; you need to understand what you want from your code.

+2
May 22 '11 at 21:32
source share

In order for Init execute as a function, your code inside the self-executing function must return a function, and the only reason for this is that you need to build a certain function that dynamically depends on some data states

 var Init = (function() { // some code return function () { // some dynamic code dependent upon your previous code }; }()); 
+2
May 22 '11 at 21:35
source share

Quick try replacing this as

 var Init = function() { my js goes here }); 

and when loading the load Init

+1
May 22 '11 at 21:33
source share

you could do as described above, but you can also do

 function Init(){...}(); 

You have nothing to stop having a named self-executing function. If you want to avoid a function called Init, you can do this, as suggested by CD Sanchez, and assign it at runtime.

(); in the end makes it self-fulfillment. Combining a function in parentheses makes it anonymous. But it seems you do not want this to be anonymous.

+1
Aug 16 2018-12-12T00:
source share

You can try to declare it as follows:

 (function Init(){ /*...*/ })(); 

But this will reduce the use of this function in the body.

Another way is to disable the declaration from execution:

 var Init = function(){ /*...*/ }, initResult = (function(){ return Init(); })(); 
0
May 22 '11 at 21:38
source share



All Articles