javascript无法调用__prototype__中的方法,说Object [object Object]没有方法'display'

时间:2011-03-17 01:56:26

标签: javascript jquery mootools

已解决,我的代码中存在错误

我正在测试一个简单游戏中的moo4q jQuery-MooTools综合(http://moo4q.com/)并遇到问题。以下是导致问题的代码片段:

// 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));

},

作为解释,this.gameObjects ['imageHolder']是一个传递给img对象的画布上下文,告诉它在哪里绘制。特别是'img.display(this.gameObject ['imageHolder']);我收到了错误

Uncaught TypeError: Object [object Object] has no method 'display'
来自Chrome的

在调用之前的行上调用的日志跟踪的输出(只是一个包装的window.console.log)是:

[Class.newClass.constructor
 file: Object
 keyword_list: Array[1]
 options: F
 __proto__: Object
   canvasContext: null
   checkResponse: function (){
   constructor: function (){
   display: function (){
   file: null
   initialize: function (){
   keyword_list: Array[0]
   log: function (){
   options: Object
   __proto__: Object

我想,也许,它会将显示屏称为 proto ?但似乎没有。 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);
}

这里的整个图片(点击动物时休息):

http://www.clumsyfingers.net/projects/game/revealer/

最相关的文件位于我打开的/ apps目录中

http://www.clumsyfingers.net/projects/game/revealer/app/

HALP?

1 个答案:

答案 0 :(得分:1)

var img = imgLib.getUniqueImage();

imgLib是一个数组/ html集合吗?如果是这样,img是一个对象?

img.display(this.gameObjects['imageHolder']);

你是原型对象吗?那是not a good idea,如果你是的话。我建议你通过一个类方法displayImage重构,然后调用它。