javascript无法在“原型”中调用方法,表示对象[Object Object]没有方法';显示';
已解决,是我的代码中的错误 我正在测试moo4qjquerymootools合成(http://moo4q.com/)在一个简单的游戏中,我遇到了一个问题。下面是导致问题的代码片段:javascript无法在“原型”中调用方法,表示对象[Object Object]没有方法';显示';,javascript,jquery,mootools,Javascript,Jquery,Mootools,已解决,是我的代码中的错误 我正在测试moo4qjquerymootools合成(http://moo4q.com/)在一个简单的游戏中,我遇到了一个问题。下面是导致问题的代码片段: // write score counter, puts up overlay, load image initGame: function () { this.app.unbind('loadingDone',this.initGame); this.setupHUD(); this.
// write score counter, puts up overlay, load image
initGame: function () {
this.app.unbind('loadingDone',this.initGame);
this.setupHUD();
this.gameObjects['scoreCounter'].html('1000');
var imgLib = this.imgs[this.gameType];
var img = imgLib.getUniqueImage();
this.log(img);
img.display(this.gameObjects['imageHolder']);
this.gameObjects['overlay'].fillRect(0, 0, 320, 460);
$(this.gameObjects['overlay'].canvas).bind('click', $.proxy(this.pokeHole, this));
},
作为说明,这个.gameObjects['imageHolder']是一个画布上下文,它传递给img对象,告诉它在哪里绘制。特别是在“img.display(this.gameObject['imageHolder'])行上;我发现了错误
Uncaught TypeError: Object [object Object] has no method 'display'
从铬
在调用之前的行上调用的日志跟踪的输出(它只是一个包装的window.console.log)是:
我想,也许,它会将显示器称为用原型机扩展的?但似乎不是。this.imgs[this.gameType]是一个类ImageContainer。getUniqueImage如下所示:
// gets a random image from the imgsShown array, then removes it
// so that subsequent calls to getUniqueImage will always
// be unique. Refills imgsShown if empty.
getUniqueImage: function() {
// copy over the img array if imgsShown array is empty
if(this.imgsShown.length == 0) this.imgsShown = this.imgs.slice(0);
var rand = Math.floor(Math.random() * this.imgsShown.length);
return this.imgsShown.splice(rand,1);
}
此处的全图(单击动物时中断):
最相关的文件位于我打开的/apps目录中
Halp?
var img=imgLib.getUniqueImage()代码>
imgLib是数组/html集合吗?如果是,img是一个对象
img.display(this.gameObjects['imageHolder'])代码>
你是一个原型对象吗?那是,如果你是的话。我建议您通过执行一个类方法displayImage进行重构,并调用它 imgLib是一个ImageCollection类实例()。我不是100%确定moo4q是如何实现的。。。不确定它是否是对象。我会调查一下,然后再和你们联系。没有人原型化对象,尤其是mootools,它的类实现是这个端口的。Mootools有一个Hash“Native”,它是一个带有原型的对象包装器(已弃用),现在它在对象类型中执行伪方法,就像。是的,不是那样,结果是我返回了一个数组,而不是对象本身。现在我知道chrome debug中的[]不仅仅是语法上的糖,它意味着您正在引用一个数组+谢谢你们两个看一看。谢谢。这是getUniqueImage函数返回值中的一个错误。我返回的是一个数组(由splice函数创建),而不是对象。作为参考,Chrome调试中的[]表示记录的参数是一个数组。
// gets a random image from the imgsShown array, then removes it
// so that subsequent calls to getUniqueImage will always
// be unique. Refills imgsShown if empty.
getUniqueImage: function() {
// copy over the img array if imgsShown array is empty
if(this.imgsShown.length == 0) this.imgsShown = this.imgs.slice(0);
var rand = Math.floor(Math.random() * this.imgsShown.length);
return this.imgsShown.splice(rand,1);
}