javascript - 为什么我必须在闭包中声明一个函数来访问闭包中定义的变量?

标签 javascript closures

为什么我必须在闭包中声明一个函数才能访问闭包中的变量?我期望能够在闭包之外定义函数,但在闭包周围定义该函数,提供所需的变量,但除非该函数实际上在闭包内定义,否则这些变量不可用。

http://jsfiddle.net/c5oba93a/1/

//reusable function defined outside of closure, I want to swap out myMethod within this function.
var reusableFunction = function () {
    //common code
    try {
        console.log(myVar);
        myMethod.call(this);
    } catch (e) {
        console.log(e);
    }
    //common code
};


var functOneForClosure = function () {
    console.log('functOne');
};
var functTwoForClosure = function () {
    console.log('functTwo');
};

//myMethod and myVar are undefined in reusableFunction
(function () {
    var myMethod = functOneForClosure;
    var myVar = 'variable in closure with functOne';
    return reusableFunction;
})()();
(function (myMethodIn) {
    var myMethod = myMethodIn;
    var myVar = 'variable in closure with functTwo';
    return reusableFunction;
})(functOneForClosure)();

//if the function is defined within the closure it behaves as expected.
var getReusableVersion =(function (myMethod) {
    var myVar = 'defining function within closure makes the difference';
    return function () {
        //common code
        try {
            console.log(myVar);
            myMethod.call(this);
        } catch (e) {
            console.log(e);
        }
        //common code
    };
});

getReusableVersion(functOneForClosure)();

最佳答案

@pherris 根据您之前的评论:这没有帮助。 IIFE 内的 this 引用了 window 对象,现在您可以使用其他属性来垃圾邮件它。但是reusableFunction中的myVar和myMethod仍然是未定义的,你必须使用this.myVarthis.myMethod来代替。比向窗口发送垃圾邮件更好的方法是将变量作为参数传递:

var reusableFunction = function (myVar, myMethod) {
    // no try/catch needed anymore
    console.log(myVar);
    if (typeof myMethod == 'function') myMethod.call(this);
    else console.log(myMethod);
};

现在有两种方法可以通过 IIFE 调用它。

第一个从 IIFE 返回函数并绑定(bind)参数,然后执行它:

(function () {
    var myMethod = functOneForClosure;
    var myVar = 'variable in closure with functOne';

    // first argument of .bind() appears in the function as 'this',
    // the others appear as fixed arguments of the function
    return reusableFunction.bind(window, myVar, myMethod);
})()();

第二种方式在 IIFE 内部调用它,因此后面的调用被删除:

(function () {
    var myMethod = functOneForClosure;
    var myVar = 'variable in closure with functOne';

    return reusableFunction(myVar, myMethod);

    // of course its possible to call on 'this', but that doesn't change anything:
    /* return reusableFunction.call(this, myVar, myMethod); */

})();

关于javascript - 为什么我必须在闭包中声明一个函数来访问闭包中定义的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26324121/

相关文章:

javascript - 是否可以从 WPF Web 浏览器控件的 Java 脚本调用 C# 方法?

javascript - 远程 JSON 的奇怪行为 Typeahead

javascript - Facebook Feed 对话框 JavaScript。描述属性已弃用。替代品?

php - 对 PHP 中任何闭包的惰性求值

c# - 指针类型数组上的 foreach 的闭包语义

javascript - 从无序列表或有序列表中添加和删除数组元素

javascript - 检测 Chrome for iOS 地址栏何时显示

swift - 在 swift 中使用闭包过滤掉无法转换为 int 的字符串

Javascript:在对象闭包中使用访问器属性

Javascript 闭包和前置后函数处理程序