Home >Web Front-end >JS Tutorial >Prototype vs. `__proto__`: What's the Real Difference?

Prototype vs. `__proto__`: What's the Real Difference?

Barbara Streisand
Barbara StreisandOriginal
2024-12-14 15:19:14615browse

Prototype vs. `__proto__`: What's the Real Difference?

Prototype and __proto__: Understanding the Distinction

In the realm of object-oriented programming, understanding the concepts of prototype and .__proto__ is crucial. While these terms may seem synonymous, they serve distinct purposes.

proto refers to the internal reference to an object's prototype, which is the object that provides the inheritance behavior for the given object. It's the actual object utilized during the lookup chain to resolve methods and properties.

On the other hand, prototype is an explicit property of the constructor function that points to the prototype object. When a new object is created using the constructor function, the __proto__ property of the new object is set to the value of the constructor's prototype.

To illustrate this distinction:

var b = new Foo(20);
var c = new Foo(30);

(new Foo).__proto__ === Foo.prototype // true
(new Foo).prototype === undefined // true

In this example, Foo.prototype is the prototype object for both b and c. The __proto__ property of b and c points to this prototype object, allowing them to inherit properties and methods from it.

It's worth noting that prototype is a read-writable property, while __proto__ is generally considered read-only. Modifying the __proto__ property directly can lead to unexpected behaviors.

The above is the detailed content of Prototype vs. `__proto__`: What's the Real Difference?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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