javascript的方法调用问题。。各路大神求解

2025-04-27 04:26:52
推荐回答(2个)
回答1:

construct.prototype.run = function () {
    ...
    var me = this;
    setInterval(function(){ me.tiao(me.i); }, 1000);
}

回答2:

就是传给SetInterval的第一个参数是匿名函数的原因,如果你在这个匿名函数中输出this,就会发现这个this其实指向window对象,也就是全局this。看了下面的你应该就明白了。

function constructor(){
this.i = 0;
this.run();
}
constructor.prototype.run = function (){
var thiz = this;
(function(){
alert(this); // 提示[object Window]
alert(thiz.i); // 提示0
})();
}
new constructor();