为什么我不使用 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?

查看:24
本文介绍了为什么我不使用 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 实际上是 Spaceship.

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天全站免登陆