Javascript - 订购功能的替代方式?
Javascript - Alternate way to order functions?
所以我正在制作我的 "Sprite" class,现在当它像这样布局时它可以正常工作(很多都是不必要的,但可能有助于你理解):
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);
}
}
基本上,我想向子classes 添加函数,而不仅仅是覆盖先前存在的函数。有人可以帮助我吗?
如果我理解你的问题,那可能纯粹是你的 Javascript 语句顺序的问题。您没有显示整个代码序列,但是当您这样做时:
Sprite.prototype = Object.create(Entity.prototype);
这将替换 Sprite 对象上的整个原型,因此如果您之前在原型上放置了任何方法,它们将被此赋值删除。如果您随后想向 Sprite 原型添加更多方法,只需在您这样做之后(而不是之前)添加它们:
Sprite.prototype = Object.create(Entity.prototype);
Sprite.prototype.render = function() {
ctx.drawImage(this.img, this.x, this.y);
}
如果您按其他顺序进行操作,将无法正常工作:
Sprite.prototype.render = function() {
ctx.drawImage(this.img, this.x, this.y);
}
// replaces the entire prototype object, wiping out any methods that were on it
Sprite.prototype = Object.create(Entity.prototype);
所以我正在制作我的 "Sprite" class,现在当它像这样布局时它可以正常工作(很多都是不必要的,但可能有助于你理解):
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);
}
}
基本上,我想向子classes 添加函数,而不仅仅是覆盖先前存在的函数。有人可以帮助我吗?
如果我理解你的问题,那可能纯粹是你的 Javascript 语句顺序的问题。您没有显示整个代码序列,但是当您这样做时:
Sprite.prototype = Object.create(Entity.prototype);
这将替换 Sprite 对象上的整个原型,因此如果您之前在原型上放置了任何方法,它们将被此赋值删除。如果您随后想向 Sprite 原型添加更多方法,只需在您这样做之后(而不是之前)添加它们:
Sprite.prototype = Object.create(Entity.prototype);
Sprite.prototype.render = function() {
ctx.drawImage(this.img, this.x, this.y);
}
如果您按其他顺序进行操作,将无法正常工作:
Sprite.prototype.render = function() {
ctx.drawImage(this.img, this.x, this.y);
}
// replaces the entire prototype object, wiping out any methods that were on it
Sprite.prototype = Object.create(Entity.prototype);