Javascript 简单方法调用中的TypeError
[已解决] 定义类Javascript 简单方法调用中的TypeError,javascript,Javascript,[已解决] 定义类 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
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
}
实际上,错误来自canvaspainer.draw
这是我做的蠢事
其他一切正常,感谢您的帮助继承函数在哪里定义?这个函数对我来说执行得很好。唯一的区别是使用$.extend_this,args代替了inherit。查看jQuery的extend看起来真正的问题在于CanvasPainter。sprite。。。你能得到你的错误的最小可运行的例子吗?你能提供inherit方法吗?在传递到inherit方法之前和之后检查这个不会有什么坏处。是的,你是对的,我在其他情况下使用它,让我检查一下,然后回来。我不确定你在使用哪个浏览器,但在Chrome中,如果你在画布上绘制的东西不是图像,则会出现消息类型错误。在你的石头对象中,您希望draw:功能完成什么?您是在尝试调用名称与关键字匹配的函数,还是在尝试定义函数对象?我没有使用jquery,因为我没有使用DOM,只使用canvas