繁体   English   中英

在这种情况下 JavaScript 闭包是如何工作的?

[英]How does JavaScript closure work in this case?

在这种情况下 JavaScript 闭包如何工作,更具体地说:最后的(i)是做什么的?

for(var i = 0; i < 10; i++) {
    (function(e) {
        setTimeout(function() {
            console.log(e);  
        }, 1000);
    })(i);
}

我也试图在我的代码中实现它,但似乎我没有做对

for (var i=0; i < len; i++) {

    var formID = document.forms["form-" + i];
    $(formID).bind("submit", validate);

    $(formID).bind("change", function(i){
        var divI = '#ind-' + i;
        $(divI).css("background-color","green");
    })(i);
}

这是一种用于围绕变量创建局部作用域的模式。 如果没有使用它,那么每次调用console.log(i)都会在 for 循环完成后记录i (10) 的值。

for(var i = 0; i < 10; i++) {
    // create new function
    (function(e) {
        // log each counter after 1 second.
        setTimeout(function() {
            console.log(e);  
        }, 1000);
    // execute it with the counter
    })(i); 
}

以上与此相同。

function foobar(e) {
    setTimeout(function() {
         console.log(e);
    }, 1000);
}

for (var i = 0; i < 10; i++) {
    (
        foobar
    )(i);
}

这里真正的问题是在循环中创建函数。 不要这样做:)

你的代码

for (var i=0; i < len; i++) {

    var formID = document.forms["form-" + i];
    $(formID).bind("submit", validate);
    // create a full closure around the block of code
    (function() {
        $(formID).bind("change", function(i){
            var divI = '#ind-' + i;
            $(divI).css("background-color","green");
        })//(i); Don't call (i) here because your just trying to execute the 
        // jQuery element as a function. You can't do this, you need to wrap 
        // an entire function around it.
    })(i);
}

但这是错误的,您想将这项工作委派给其他人。

function makeGreen(form, i) {
    $(form).change(function() {
         $("#ind-"+i).css("background-color", "green");
    });
}

for (var i=0; i < len; i++) {
    var formID = document.forms["form-" + i];
    $(formID).bind("submit", validate);
    // call a helper function which binds the change handler to the correct i
    makeGreen(formID, i);
}

如果你想变得聪明一点,你可以去掉这些匿名函数

function makeGreen() {
     var divId = $(this).data("div-id");
     $(divId).css("background-color", "green");
}

for (var i=0; i < len; i++) {
    $(document.forms["form-" + i])
        .bind("submit", validate)
        // store i on the form element
        .data("div-id", "#ind-" + i)
        // use a single event handler that gets the divId out of the form.
        .change(makeGreen);
}

编辑

( // contain the function we create.
    function(parameterA) {
         window.alert(parameterA);
    }
) // this now points to a function
("alertMessage"); // call it as a function.

是相同的

( // contain the window.alert function
    window.alert
) // it now points to a function
("alertMessage"); // call it as a function

虽然不是对关闭问题的直接回答,但这是我对这个问题的看法。

我会重新编写逻辑以避免需要关闭(因为它的要求似乎过于复杂

表单命名有规律这一事实使事情变得非常容易

$('form[id^="form-"]').submit(validate)
     .change(function(){
          var divI = '#ind-' + this.id.replace('form-','');
          $(divI).css("background-color","green");
     });

演示http://jsfiddle.net/gaby/q8WxV/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM