所以我做我的“雪碧”级,而现在它时,它的布局是这样工作正常(这是不必要的很多,但可能会帮助你理解):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