Javascript Javascript自执行函数“不是函数”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6090912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript self executing function "is not a function"
提问by Phillip Senn
I have:
我有:
var Init = (function() {
my js goes here
})();
And my js executes correctly when the page is loaded. I also have:
加载页面时,我的 js 正确执行。我也有:
$('form :checkbox').change(function() {
Init();
});
But firebug says Init is not a function.
但是萤火虫说 Init 不是一个函数。
回答by
It isn't a function.
它不是一个函数。
(function() {
...
})()
evaluates the anonymous function right then. And the result of the evaluation apparently does not return a function-objectin 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 which will "execute something once" and then "return that something to execute later". (See "You can either [assign] a function or call it; you can't do both..."from Slaks answer.) However, I wouldn't do it like this.
这是一个函数,它将“执行一次”然后“返回稍后执行的内容”。(请参阅Slaks 回答中的“您可以[分配] 一个函数或调用它;您不能同时执行这两个操作……”。)但是,我不会这样做。
Init = (function () {
function Init () {
alert("whee!")
}
Init()
return Init
})()
Init()
Here is another solution (much shorter/cleaner) from CD Sanchez (see comment) which takes advantage of the fact that an assignment evaluates to the assigned value:
这是 CD Sanchez(见评论)的另一个解决方案(更短/更干净),它利用了赋值计算为指定值的事实:
var Init; (Init = function Init () {
alert ("wee");
})()
回答by SLaks
Init
isn't a function; it's the result of calling the function.
Init
不是函数;这是调用函数的结果。
You can either create a function or call it; you can't do both at once.
您可以创建一个函数或调用它;你不能同时做这两件事。
Technically, you could fix that by adding return arguments.callee;
to return the function from the call.
However, that's a dumb idea.
从技术上讲,您可以通过添加return arguments.callee;
从调用中返回函数来解决这个问题。
然而,这是一个愚蠢的想法。
You probably shouldn't be calling the function; you need to understand what you want your code to do.
您可能不应该调用该函数;您需要了解您希望代码做什么。
回答by mVChr
In order for Init
to execute as a function, your code within the self-executing function must return a function, and the only reason to do this is if you need to construct a specific function dynamically dependent upon some data states:
为了Init
作为函数执行,自执行函数中的代码必须返回一个函数,这样做的唯一原因是如果您需要根据某些数据状态动态构建特定函数:
var Init = (function() {
// some code
return function () {
// some dynamic code dependent upon your previous code
};
}());
回答by fijiaaron
you could do as above, but you could also do
你可以像上面那样做,但你也可以做
function Init(){...}();
There's nothing to stop you from having a named self-executing function. If you want to avoid having a function named Init, you can do as CD Sanchez suggested and assign it in the execution.
没有什么可以阻止您拥有一个命名的自执行函数。如果你想避免有一个名为 Init 的函数,你可以按照 CD Sanchez 的建议去做,并在执行中分配它。
The (); at the end makes it self executing. Wrapping the function in parentheses makes it anonymous. But it seems that you don't want it to be anonymous.
这 (); 最后使其自我执行。将函数括在括号中使其匿名。但似乎你不希望它是匿名的。
回答by Rizwan Sharif
Quick one Try replacing like this
快速一 尝试像这样更换
var Init = function() {
my js goes here
});
and on load call Init
并在加载时调用 Init
回答by dev-null-dweller
You may try declaring it this way:
你可以尝试这样声明:
(function Init(){
/*...*/
})();
But this will reduce usage of this function into it's body
但这将减少此功能在其主体中的使用
Other way is to separate declaration from execution:
另一种方法是将声明与执行分开:
var Init = function(){
/*...*/
},
initResult = (function(){ return Init(); })();