首先看一段代码
var obj = { init: function(){ this.name = 'obj'; setTimeout(this.render, 10); }, render: function(){ console.log(this,this.name); } }; obj.init();
输出this为window,this.name为空.
原因是setTimeout的执行环境和调用环境是分开的。
继续看:
var obj = { init: function(){ this.name = 'obj'; setTimeout(console.log(this.name), 10); }, render: function(){ console.log(this,this.name); } }; obj.init();
输出 “obj”