JavaScript:Object[Object global]没有方法

JavaScript:Object[Object global]没有方法,object,javascript,extend,Object,Javascript,Extend,我在这里看到过类似的问题,但没有一个解决方案解决了我的问题。我试图扩展PixiJS的BitmapText类来创建一个通用文本对象: OS7.Text = function(string, x, y) { PIXI.BitmapText.call(this, string, {font:"12px Chicago"}); this.position.x = x; this.position.y = y; } OS7.Text.prototype = Object.crea

我在这里看到过类似的问题,但没有一个解决方案解决了我的问题。我试图扩展PixiJS的BitmapText类来创建一个通用文本对象:

OS7.Text = function(string, x, y)
{
    PIXI.BitmapText.call(this, string, {font:"12px Chicago"});
    this.position.x = x;
    this.position.y = y;
}

OS7.Text.prototype = Object.create( PIXI.BitmapText.prototype );
OS7.Text.prototype.constructor = OS7.Text;
然后将其扩展为一个每秒更新一次的简单时钟:

OS7.Time = function()
{
    OS7.Text.call(this, "00:00 AM", 571, 5);
    this.position.x = 571 - this.textWidth;
    this.updateTime();
    this.timeFunc = this.updateTime();
    window.setInterval(this.timeFunc, 1000);
};

OS7.Time.prototype = Object.create(OS7.Text.prototype);
OS7.Time.prototype.constructor = OS7.Time;

OS7.Time.prototype.updateTime = function()
{
    this.prevText = this.text;
    this.date = new Date();
    this.hour = this.date.getHours();
    this.minute = this.date.getMinutes();
    this.zero = "";
    this.ampm = "AM";

    if ( this.hour > 12 )
    {
        this.hour -= 12;
        this.ampm = "PM";
    }

    if ( this.hour === 0 )
    {
        this.hour = 12;
    }

    if ( this.minute < 10 )
    {
        this.zero = "0";
    }

    this.setText( this.hour + ":" + this.zero + this.minute + " " + this.ampm );

    if ( this.prevText !== this.text )
    {
        this.updateText();
    }
};
OS7.Time=function()
{
OS7.Text.call(这是“00:00am”,571,5);
this.position.x=571-this.textWidth;
this.updateTime();
this.timeFunc=this.updateTime();
window.setInterval(this.timeFunc,1000);
};
OS7.Time.prototype=Object.create(OS7.Text.prototype);
OS7.Time.prototype.constructor=OS7.Time;
OS7.Time.prototype.updateTime=函数()
{
this.prevText=this.text;
this.date=新日期();
this.hour=this.date.getHours();
this.minute=this.date.getMinutes();
此为0.zero=“”;
this.ampm=“AM”;
如果(本小时>12)
{
这个小时-=12;
this.ampm=“PM”;
}
如果(this.hour==0)
{
这个小时=12;
}
如果(本分钟<10)
{
这是0.zero=“0”;
}
this.setText(this.hour+:“+this.zero+this.minute+”“+this.ampm);
if(this.prevText!==this.text)
{
this.updateText();
}
};
不管怎样,我得到的错误是
Object[Object global]没有方法updateText
,即使该函数位于
PIXI.BitmapText
中。更不用说整个
timeFunc
似乎是多余的,但在此之前我得到了错误
Object[Object global]没有方法updateTime


为什么会出现此错误?

在时间间隔上调用函数时,
this
的值不会是对象的实例。您必须将其包装在函数中:

var self = this;
window.setInterval(function() { self.updateTime(); }, 1000);

这条线看起来可疑:

this.timeFunc = this.updateTime();
timeFunc
将是
未定义的
,因为您正在调用
updateTime
,它不会返回任何内容。另外,从计时器调用的函数将具有
窗口
,而不是绑定到此的对象。如果要保留对象参照,需要使用
bind

this.timeFunc = this.updateTime.bind(this);

是的,这也行;它实现了与包装函数相同的目标(好的
.bind()
为您创建包装)因为
timeFunc
无论如何都有点像黑客。非常感谢。看这是非常有帮助的菲利克斯,谢谢!