爲什麼我必須在閉包中聲明一個函數才能訪問閉包中的變量?我期望我能夠在閉包之外定義函數,但是可以在閉包周圍定義函數,提供它所需的變量,但變量不可用,除非函數實際上在閉包中定義。爲什麼我必須在閉包中定義一個閉包訪問變量時聲明一個函數?
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)();
在函數內部聲明的var在這個函數之外是不可見的,並且在函數完成執行時被清除。 – 2014-10-12 10:27:57
你在哪裏看到發生@MartinErnst? – pherris 2014-10-12 10:30:04
有兩個IIFE定義'myVar'和'myMethod'並返回''reuasableFunction''然後執行。當IIFE的返回變量被清除時,因爲它們沒有被傳遞給'reusableFunction'。此外,在定義了'reusableFunction'的'地方',變量不是'可見的'。 – 2014-10-12 10:38:33