2012-03-02 42 views
0

[解决]类型错误在一个简单的方法调用

定义的类

var Stone = function(args){ 

    var _this = this; 
    inherit(_this, args); //just copy all attributes from args to _this 

    _this.w = 32; 
    _this.h = 32; 
    _this.obstacle = true; 
    _this.padding = 0; 
    _this.sprite = { 
     x: 32, 
     y: 0 
    }; 

    _this.draw = function(){ 
     console.log(_this); 
     CanvasPainter.sprite("background", _this.sprite.x, _this.sprite.y, 
          _this.x, _this.y, _this.w, _this.h); 
    }; 
}; 

定义对象

var stone = new Stone({ 
    x: 480, 
    y: 320 
}); 

调用方法

stone.draw(); //throws "TypeError: Type error" 

我已经知道我正在做一些愚蠢的事情。你能帮我找到它吗?

这里是石对象:

stone = { 
    draw: function(), 
    h: 32, 
    obstacle: true, 
    padding: 0, 
    sprite: {x: 32, y: 0}, 
    w: 32, 
    x: 480, 
    y: 320 
} 

其实错误来自CanvasPainter.draw 是一个愚蠢的事情I`ve做

一切是好的,感谢您的帮助

+3

看起来像真正的问题是在CanvasPainter.sprite中...你能得到一个最小的可运行的错误示例吗? – hugomg 2012-03-02 17:58:47

+0

你能提供继承方法吗?在传递给继承方法之前和之后检查_this都不会受到伤害。 – Mathachew 2012-03-02 17:59:33

+0

是的,你是对的,好吧,我在其他情况下使用它,让我检查出来,并会回来 – melanke 2012-03-02 18:01:29

回答

0

inherit函数在哪里定义?这个函数对我来说执行得很好。唯一的区别是inherit,我用$.extend(_this, args)。 (请参阅jQuery的扩展http://api.jquery.com/jQuery.extend/

+0

我不使用jquery,因为我没有使用DOM,只有canvas – melanke 2012-03-02 18:25:56

相关问题