看看下面的代码:
var pocket = {
cash: 1000,
showCash: function() {
return this.cash;
}
};
var thomas = {
name: "Thomas",
work: function() {
console.log('I don\'t want to, but I have to... work!');
},
cash: 5000
};
var bird = {
fly: function() {
console.log('I\'m flying!');
}
};
console.log(pocket.showCash());
thomas.showCash = pocket.showCash;
console.log(thomas.showCash());
bird.showCash = pocket.showCash;
console.log(bird.showCash());
var cash = 20;
var showCashFun = pocket.showCash;
console.log(showCashFun());
代码非常简单,显示引擎如何解释this
关键字。当我在 chrome 控制台中运行它时,我得到以下输出:
1000
5000
undefined
20
没关系 - 我明白这一切。但是当我在 node.js 控制台中运行它时,我得到:
1000
5000
undefined
undefined
chrome 和 node.js 都使用 v8。怎么会有这样的区别?
编辑:如果有什么不同,我的节点版本是v0.10.8
chrome 是27.0.1453.93
.