Warum würde ich nicht Child.prototype = Parent.Prototype anstelle von Child.prototype = new Parent () verwenden? für die Javascript-Vererbung?

Ich verstehe dieses Verhalten in Javascript für die Vererbung nicht. Ich habe es immer so definiert gesehen:

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

Aber in meinem Fall sind diese Zeilen:

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

Wenn ich ein console.log (this) in meinem Spaceship-Konstruktor mache, kann ich sehen, dass dasproto Die Eigenschaft wird anstelle von GameObject auf Spaceship gesetzt. Wenn ich sie entferne, wird sie auf GameObject gesetzt.

Und wenn ich benutze:

 Spaceship.prototype = GameObject.prototype;

Damit habe ich keine Probleme mehr. Der Grund, warum dies mich blockiert, ist, dass ich ein anderes Objekt mit einer add () -Methode habe und prüft, ob das Objekt mit dem folgenden Code in GameObject eintritt:

 if(object instanceof GameObject)

Ich verstehe nicht, was diese beiden Zeilen wahrscheinlich ändern können, sodass die Vererbung unterbrochen wird, wenn sie vorhanden sind, und ich bin nicht sicher, ob die Vererbung auf die zweite Art und Weise gut ist. Könnte mich bitte jemand darüber aufklären? :)

Antworten auf die Frage(3)

Ihre Antwort auf die Frage