Javascript 自执行应该可以工作吗?

发布于 2024-12-13 04:27:22 字数 954 浏览 0 评论 0原文

我有这段代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

述情 2024-12-20 04:27:22

编译器需要区分函数声明语句和函数定义表达式。您只能在表达式中调用函数。

当关键字function出现在'='或'('之后之后,编译器知道这一定是一个函数定义表达式(因为“=”或“(”之后只允许表达式而不是语句)并允许您立即调用它。当关键字函数开始< /strong> 一个新语句,编译器假定它是一个函数声明语句,因此您不能立即调用该函数

请注意,函数声明语句要求您命名该函数,这允许您稍后调用它。函数定义表达式中的函数名称(您可以立即调用它们)。

关键

粗体字体均由 OP 制作:这些粗体字是理解的

this is the most logical Explanation after a lot of tests.

所有 .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.

this is the most logical Explanation after a lot of tests.

See This

http://jsbin.com/imetan/edit#javascript,html

素染倾城色 2024-12-20 04:27:22

(i) 部分将 i 的值作为参数传递给声明为 onclick事件处理程序,仅此而已。它并没有真正“做”任何事情。

抱歉,我纠正了。(i) 正在执行传递 i 的函数代码> 作为参数。这导致 onclick 实际上是这样的函数:

function (num) {
    alert(num);
};

如果您尝试像这样从函数中访问变量 i ,您总是会得到它的最终值,在本例中为 5。

The (i) part is passing the value of i as parameter to the anonymous function (function without name) which is declared as the onclick event handler, that's all. It doesn't really "do" anything.

Sorry, I stand corrected.. the (i) is executing the function passing i as parameter. This results in the onclick actually being such function:

function (num) {
    alert(num);
};

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文