简体   繁体   English

Javascript-排序函数的替代方法?

[英]Javascript - Alternate way to order functions?

So I'm making my "Sprite" class, and right now it works properly when it's laid out like this (alot of this is unnecessary, but might help you understand): 因此,我正在制作我的“ Sprite”类,现在,当这样布置时,它可以正常工作(这是不必要的,但可能会帮助您理解):

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();
}

But what I want to do is make Sprite 's render function just like Entity 's confirmType function, outside the constructor. 但是我想做的是在构造函数之外使Spriterender函数像EntityconfirmType函数一样。

What I want to do is this: 我想做的是这样的:

function Sprite(tname, x, y, src)
    {
        ...
    }

    Sprite.prototype.render = function()
    {
        ctx.drawImage(this.img, this.x, this.y);
    }

Not: 不:

function Sprite(tname, x, y, src)
    {
        ...

        this.render = function()
        {
            ctx.drawImage(this.img, this.x, this.y);
        }
    }

Basically, I want to add functions to subclasses, not just override preexisting ones. 基本上,我想将功能添加到子类中,而不仅仅是覆盖现有功能。 Can someone help me? 有人能帮我吗?

If I understand your issue, it may be purely an issue of the order of your Javascript statements. 如果我理解您的问题,则可能纯粹是Javascript语句顺序的问题。 You don't show the whole sequence of code, but when you do this: 您不会显示完整的代码序列,但是在执行此操作时:

 Sprite.prototype = Object.create(Entity.prototype);

That replaces the entire prototype on the Sprite object so if you had previously put any methods on the prototype, they would be wiped out by this assignment. 这将替换Sprite对象上的整个原型,因此,如果您以前在原型上放置了任何方法,则此分配将清除它们。 If you then want to add more methods to the Sprite prototype, just add them after you do that (not before): 如果您随后想向Sprite原型添加更多方法,则只需在执行此操作后添加它们(而不是之前):

 Sprite.prototype = Object.create(Entity.prototype);
 Sprite.prototype.render = function() {
    ctx.drawImage(this.img, this.x, this.y);
 }

If you did them in the other order, it would not work: 如果您以其他顺序进行操作,则将无法正常工作:

 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM