Open goldEli opened 4 years ago
this
window
globe
new
var video = { name: "coco", tags: [1,2,3], play: function() { this.tags.forEach(function(tag) { console.log(this.name, tag) }) }, country: { name: "china", print: function() { console.log(this.name) } } } video.play() // window 1 // window 2 // window 3 video.country.print() // china
forEach
this.tags
print
this
指向(window
或者globe
)this
指向改对象new
,this
指向新的实例this
指向window
,是因为forEach
方法传入的就是一个普通函数。this.tags
指向调用它的对象。print
方法中的this
也是指向调用它最近的对象。