所以我做我的「雪碧」級,而現在它時,它的佈局是這樣工作正常(這是不必要的很多,但可能會幫助你理解):JavaScript - 訂購功能的替代方法?
function Entity(tname)
{
if (typeof (tname) === 'undefined') tname = "Entity";
this.tname = tname;
}
Entity.prototype.confirmType = function(tname)
{
if (this.tname === tname) return true;
else return false;
}
Entity.prototype.constructor = Entity;
function Sprite(tname, x, y, src)
{
this.parent.constructor.call(this, tname);
this.x = x;
this.y = y;
this.img = new Image();
this.img.src = src;
this.render = function()
{
ctx.drawImage(this.img, this.x, this.y);
}
}
Sprite.prototype = Object.create(Entity.prototype);
Sprite.prototype.constructor = Sprite;
Sprite.prototype.parent = Entity.prototype;
var sprite = new Sprite("Lucario", 400, 400, "img/slot.png");
var update = function()
{
sprite.render();
}
但我想要什麼要做的就是Sprite
的render
函數就像Entity
的confirmType
函數,在構造函數之外。
我想要做的是這樣的:
function Sprite(tname, x, y, src)
{
...
}
Sprite.prototype.render = function()
{
ctx.drawImage(this.img, this.x, this.y);
}
不:
function Sprite(tname, x, y, src)
{
...
this.render = function()
{
ctx.drawImage(this.img, this.x, this.y);
}
}
基本上,我想要的功能添加到子類,而不只是覆蓋先前存在的。有人能幫我嗎?
這是真的如何縮進你的代碼?只是好奇你爲什麼選擇這種風格? – jfriend00 2015-02-09 00:21:48
不,有些在複製/粘貼過程中遇到了問題。最後一個「var update = function()」就是它通常的樣子。 – 2015-02-09 00:36:53