Javascript 自执行应该可以工作吗?
我有这段代码:
function (i)
{
alert(i);
}(3);
它不起作用,所以在 StackOverFlow 问题之后 - 我将其更改为:
(function(i){ alert(i); })(3);
它起作用了。
我必须()包装所有代码。
但是后来我在其他网站上看到了这段代码:
function addLinks () {
for (var i=0, link; i<5; i++) {
link = document.createElement("a");
link.innerHTML = "Link " + i;
link.onclick = function (num) {
return function () {
alert(num);
};
}(i); // <=== What about this ? there is no () wrapper... so how does it work ?
document.body.appendChild(link);
}
}
window.onload = addLinks;
我想问(i)
部分的作用是什么? 它正在执行某些事情吗?
如果它确实如此,为什么它不采用以下模式:
(function(i){alert(i); })(3);
我的意思是其中是包装器() ?
I had this code :
function (i)
{
alert(i);
}(3);
And it wasn't working , So After StackOverFlow Question - I changed it to :
(function(i){ alert(i); })(3);
And it works.
I had to () wrap all the code.
But then I saw this code on other site :
function addLinks () {
for (var i=0, link; i<5; i++) {
link = document.createElement("a");
link.innerHTML = "Link " + i;
link.onclick = function (num) {
return function () {
alert(num);
};
}(i); // <=== What about this ? there is no () wrapper... so how does it work ?
document.body.appendChild(link);
}
}
window.onload = addLinks;
I wanted to ask what is the role for the (i)
part ? Is it executing something ?
And if it Does why Its not in a pattern of :
(function(i){ alert(i); })(3);
I mean where is the wrappers of () ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编译器需要区分函数声明语句和函数定义表达式。您只能在表达式中调用函数。
当关键字function出现在'='或'('之后之后,编译器知道这一定是一个函数定义表达式(因为“=”或“(”之后只允许表达式而不是语句)并允许您立即调用它。当关键字函数开始< /strong> 一个新语句,编译器假定它是一个函数声明语句,因此您不能立即调用该函数。
请注意,函数声明语句要求您命名该函数,这允许您稍后调用它。函数定义表达式中的函数名称(您可以立即调用它们)。
关键
粗体字体均由 OP 制作:这些粗体字是理解的
。
所有 .com/imetan/edit#javascript,html" rel="nofollow">http://jsbin.com/imetan/edit#javascript,html
The compiler needs to differentiate between function declaration statement and function definition expression. You can only call functions in expressions.
When the keyword function appears after '=' or '(' the compiler knows this must be a function definition expression (since only expressions and not statements are allowed after '=' or '(') and allows you to call it immediately. When the keyword function starts a new statement the compiler assumes it to be a function declaration statement and hence you can't call the function immediately.
Note that function declaration statement requires you to name the function. This allows you to call it later. You may omit the function name in function definition expressions (and you can call them immediately).
Edited
all the bold fonts were made by Royi Namir the OP: those bold words are the key for understanding.
See This
http://jsbin.com/imetan/edit#javascript,html
(i) 部分将i
的值作为参数传递给声明为onclick事件处理程序,仅此而已。它并没有真正“做”任何事情。
抱歉,我纠正了。
(i)
正在执行传递i 的函数代码> 作为参数。这导致
onclick
实际上是这样的函数:如果您尝试像这样从函数中访问变量
i
,您总是会得到它的最终值,在本例中为 5。The(i)
part is passing the value ofi
as parameter to the anonymous function (function without name) which is declared as theonclick
event handler, that's all. It doesn't really "do" anything.Sorry, I stand corrected.. the
(i)
is executing the function passingi
as parameter. This results in theonclick
actually being such function:If you will try to access the variable
i
just like this from the function you'll always end up with its final value, 5 in this case.