<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head><body>
<script type="text/javascript">
function test(name){
this.name = name;
}
test.prototype = {
age:0,
alertt:function(i){
var self = this;
document.write(this.name);
document.write(self.age);
alert(i);
},
dd:function(){
var self = this;
var i = 1;
this.alertt(this.name);
setInterval(function(){
i++;
this.alertt(i);
},2000)
}
}
var ff = new test('chaofan');
ff.dd();
</script>
</body>
</html>
为什么在
setInterval(function(){
i++;
this.alertt(i);
},2000)
处的this.alertt(i);
是未定义的,要怎么解决这个问题

解决方案 »

  1.   

    错误是:this.alertt is not a function
      

  2.   

    this.alertt ??啥东西啊?
      

  3.   

    alert把 是不是多了一个t啊 
    this.alertt is not a function意思this.alertt 不是一个方法~~
      

  4.   

    改成 
    setInterval(function(){
    i++;
    self.alertt(i);
    },2000)===========================
    在function()中,this是没有定义的,应为function()并不是test的方法
      

  5.   

    var self = this;
    你已经定义了 self=this;
    把  dd:function(){}
    内的   this.alertt(i)  改为 self.alertt(i)