閉包是從其封閉環境中繼承變量的函數。所以,讓我們看看下面的代碼:回調中的閉包
binaryServer = BinaryServer({port: 9001});
binaryServer.on('connection', function(client) {
console.log("new connection");
client.on('stream', function(stream, meta) {
stream.on('data', function(data){
//actions
});
stream.on('end', function() {
//actions
});
});
});
所以,我是正確的,如果我說:「function(data){//actions}
是一個閉包,因爲繼承了封閉環境(如對象client
)的變量」?因爲我的經歷就是這樣發生的事情。
您可能還聽到一個功能 「關閉了」 特定變量。 – zzzzBov
通常的JS'這個'注意事項適用,例如,閉包中的'this'不會是定義上下文中的'this'。 –