Home > Web Front-end > JS Tutorial > Why Doesn't JavaScript's Constructor Property Update in Prototypal Inheritance?

Why Doesn't JavaScript's Constructor Property Update in Prototypal Inheritance?

DDD
Release: 2024-12-06 18:34:12
Original
578 people have browsed it

Why Doesn't JavaScript's Constructor Property Update in Prototypal Inheritance?

Understanding JavaScript Inheritance and the Constructor Property

In JavaScript, inheritance is achieved through prototypal inheritance, where objects inherit properties and methods from other objects through their prototype chain. The constructor property plays a crucial role in this mechanism.

Inheritance and the constructor Property

Consider the following code:

function a() {}
function b() {}
function c() {}

b.prototype = new a();
c.prototype = new b();

console.log((new a()).constructor); //a()
console.log((new b()).constructor); //a()
console.log((new c()).constructor); //a()
Copy after login

Why isn't the constructor updated for b and c? This is because in JavaScript, the constructor property is not assigned to the instance directly; instead, it resides on the prototype object. It stores a reference to the constructor function that created the object. In the example above, the prototype of b and c is set to an instance of a, which is why the constructor for all three instances is reported as a().

Best Practices for Updating the constructor

To update the constructor property for inherited classes, a common approach is to use an intermediate function:

function base() {}
function derive() {
  derive.superclass = base.prototype;
  derive.prototype = new derive.superclass();

  derive.prototype.constructor = derive;
}
Copy after login

This technique ensures that the constructor property of derive instances is correctly set to the derive function.

Instanceof and the Prototype Chain

While the (new c()).constructor is equal to a(), it's still possible for instanceof to correctly identify new c() as an instance of c. This is because instanceof checks the prototype chain of the instance object. In this case, the prototype chain for new c() leads back to the c prototype.

console.log(new a() instanceof a); //true
console.log(new b() instanceof b); //true
console.log(new c() instanceof c); //true
Copy after login

Conclusion

JavaScript's inheritance and the constructor property interactions can be complex, but understanding them is crucial for effective object-oriented programming in JavaScript. By grasping the concepts outlined in this article, you can write robust and reusable code that leverages inheritance and the prototype chain effectively.

The above is the detailed content of Why Doesn't JavaScript's Constructor Property Update in Prototypal Inheritance?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template