我在測試node.js中的回調機制以查看引發回調的上下文。在運行下面的代碼,我注意到一個奇怪的行爲,我不知道你能不能解釋一下:node.js參數隱藏
var outID =2;
var closure = function(){
var that = {};
that.id = 1;
outID = 3; //if line is commented out outID will always be 2.
that.inside = function(cb){
console.log("level 0");
console.log("thatID:" +that.id);
console.log("outID:" +outID);
setTimeout(function(){
console.log("level 1");
console.log("thatID:" +that.id);
console.log("outID:" +outID);
setTimeout(function(){
setTimeout(cb,0,that.id);
},0);
}, 0);
};
return that;
};
var level3 = function(id){
console.log("level 100S");
console.log("id " + id);
console.log(outID); // --- Interesting value is 3.
};
var cl = new closure();
cl.inside(level3);
輸出是:
node: no process found
level 0
thatID:1
outID:3
level 1
thatID:1
outID:3
level 100S
id 1
3
[Finished in 0.1s]
爲什麼是最後一個值3,而不是2?
爲什麼當你將它設置爲3時,在你調用的構造函數中它的值應該是2 ? – JohnnyHK
是的,我明白這是因爲我寫的。我只是不知道在運行timeOut時node.js如何知道這些值(符號?)? – qballer