为什么我不使用Child.prototype = Parent.Prototype而不是Child.prototype = new Parent();对于Javascript继承? [英] Why wouldn't I use Child.prototype = Parent.Prototype rather than Child.prototype = new Parent(); for Javascript inheritance?

查看:706
本文介绍了为什么我不使用Child.prototype = Parent.Prototype而不是Child.prototype = new Parent();对于Javascript继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白javascript中继承的这种行为我总是看到它定义如下:

I don't understand this behavior in javascript for inheritance I've always seen it defined like so :

function GameObject(oImg, x, y) {

    this.x = x;
    this.y = y;
    this.img = oImg;

    this.hit = new Object();
    this.hitBox.x = x;
    this.hitBox.y = y;
    this.hitBox.width = oImg.width;
    this.hitBox.height = oImg.height;

}

Spaceship.prototype = new GameObject();
Spaceship.prototype.constructor = Spaceship;

function Spaceship(){
    console.log("instantiate ship");
    GameObject.apply(this, arguments);
    this.vx = 0;
    this.vy = 0;
    this.speed = 3;
    this.friction = 0.94;
}

但就我而言,这些行:

    this.hitBox.width = oImg.width;
    this.hitBox.height = oImg.height;

当我在我的Spaceship构造函数中执行console.log(this)时,我可以看到 proto 属性设置为Spaceship而不是GameObject,如果我删除它们,则设置为GameObject。

When I do a console.log(this) in my Spaceship constructor, I can see that the proto property is set to Spaceship instead of GameObject, if I remove them, it is set to GameObject.

如果我使用:

 Spaceship.prototype = GameObject.prototype;

我没有更多问题。这阻止我的原因是我有另一个带有add()方法的对象,它使用以下代码检查GameObject的对象:

I have no more problems with that. The reason that this blocks me is that I have another object with an add() method and it checks that the object inerhits of GameObject with this code :

 if(object instanceof GameObject)

我不明白这两行是什么可能会改变,以便继承在它们存在时被破坏,我不确定继承是第二种方式是好的。有人可以告诉我这件事吗? :)

I don't understand what those two lines can probably change so that inheritance is broken when they are present and I'm not sure doing inheritance the second way is good. Could someone enlighten me about this please ? :)

推荐答案

如果你这样做

Spaceship.prototype = GameObject.prototype;

然后他们都引用同一个对象,所以你可能还有 GameObject ,如果您向 Spaceship.prototype 添加内容,它将被添加到 GameObject.prototype 。您可以在分配后通过向 Spaceship.prototype 添加内容来轻松测试它。 例如,您可以看到 GameObject.prototype.constructor 实际上是太空飞船

Then they both refer to the same object, so you might as well have everything in GameObject, if you add something to Spaceship.prototype, it will be added to GameObject.prototype as well. You can easily test it by adding something to Spaceship.prototype after the assignment. For example, in your case you can see that GameObject.prototype.constructor is actually Spaceship.

至于

Spaceship.prototype = new GameObject();

这会调用可能产生不良副作用的构造函数,您更愿意使用:

This invokes the constructor which might have undesired side effects, you rather want to use:

Spaceship.prototype = Object.create(GameObject.prototype);

这里使用的 Object.create 功能在这里归结为:

Where the used Object.create functionality here comes down to:

Object.create = function( proto ) {
    function f(){}
    f.prototype = proto;
    return new f;
};

现代浏览器已经具备此功能。

Modern browsers already have the function though.

这篇关于为什么我不使用Child.prototype = Parent.Prototype而不是Child.prototype = new Parent();对于Javascript继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆