Rumah > hujung hadapan web > tutorial js > Bilakah Anda Harus Mengelak `Child.prototype = Parent.Prototype` dalam Pewarisan JavaScript?

Bilakah Anda Harus Mengelak `Child.prototype = Parent.Prototype` dalam Pewarisan JavaScript?

Patricia Arquette
Lepaskan: 2024-11-14 10:28:02
asal
452 orang telah melayarinya

When Should You Avoid `Child.prototype = Parent.Prototype` in JavaScript Inheritance?

When to Avoid Child.prototype = Parent.Prototype in JavaScript Inheritance

While it's common practice to inherit in JavaScript using Child.prototype = new Parent();, there are exceptions where Child.prototype = Parent.Prototype may cause unintended behaviors.

Consider the following snippet:

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;
}
Salin selepas log masuk

Upon inspecting the Spaceship constructor, you'll notice that its __proto__ property points to Spaceship, not GameObject. This is because the lines:

this.hitBox.width = oImg.width;
this.hitBox.height = oImg.height;
Salin selepas log masuk

assign properties directly to this.hitBox, which doesn't exist in the GameObject prototype. This behavior is problematic because it:

  • Breaks inheritance: When using Child.prototype = Parent.Prototype, the Child prototype doesn't inherit any new functions added to the Parent prototype.
  • Violates the instanceof check: As the Spaceship instance's __proto__ points to Spaceship instead of GameObject, checking if(object instanceof GameObject) will fail.

Why Use Child.prototype = new Parent(); Instead

Child.prototype = new Parent(); invokes the constructor of the Parent class and assigns its newly created instance to the Child prototype. This ensures that:

  • Inheritance is preserved: Any new functions added to the Parent prototype will automatically be inherited by the Child.
  • instanceof checks are accurate: The __proto__ of the Child instance points to the Parent prototype, allowing the correct instanceof check.

Alternative Solution

In modern browsers that support Object.create, you can use Spaceship.prototype = Object.create(GameObject.prototype); to create the child prototype. This is functionally equivalent to Child.prototype = new Parent(); but is more concise and avoids the unnecessary constructor call.

Atas ialah kandungan terperinci Bilakah Anda Harus Mengelak `Child.prototype = Parent.Prototype` dalam Pewarisan JavaScript?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan