【发布时间】:2017-04-25 21:38:07
【问题描述】:
为什么7行返回对象window?
为什么不是运动对象?
var sport = {
caption: "2017",
players : [{"name":"cat"},{"name":"dog"}] ,
show: function() {
this.players.forEach(function(entry) {
console.log(entry.name);
console.log(this);//window
});
}
}
sport.show();
【问题讨论】:
-
使用箭头功能。
-
this 的范围取决于执行上下文并且是后期绑定的。搜索 SO 和网络,您会发现很多关于此的讨论。
-
"如果给forEach()提供了thisArg参数,则作为回调的this值。否则,将undefined的值作为它的this值。回调最终可观察到的this值是根据确定函数看到的 this 的通常规则确定。” - developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
-
"use strict"; -
为什么是
sport对象而不是sport.players数组?
标签: javascript