![]() |
VOOZH | about |
In JavaScript, objects can inherit properties and methods from another object, creating a prototype chain. This means when a property is accessed on an object, JavaScript first looks for it on the object itself (known as own properties). If not found, it checks the object's prototype and continues up the prototype chain until the property is found or the chain ends at null.
Custom toString method [object Object]
Every object in JavaScript has an internal link called [[Prototype]], which refers to its prototype object. The prototype can also have its own prototype, creating a chain known as the prototype chain.
1 3 undefined
When accessing a property on an object
10 20
If an object has its own property with the same name as an inherited property, the own property takes precedence. This is called property shadowing.
10 42
JavaScript's prototype chain is dynamic, meaning you can modify the prototype of an object after it has been created, and the changes are reflected in all objects inheriting from it.
Added later
To determine whether a property is an own property or an inherited property, you can use:
true false true false
Inherited properties can also include methods, allowing you to define reusable functionality across objects.
Hello, Parent
The prototype chain ends when a prototype is null. For example, Object.prototype is the default prototype for all objects and has null as its prototype.
null undefined
Object inheritance is commonly used in JavaScript to create hierarchies, where shared functionality is defined on a prototype and inherited by multiple objects.
Dog makes a sound. Cat makes a sound.
Accessing properties high in the prototype chain or properties that don’t exist can impact performance.
Use hasOwnProperty() to avoid iterating over inherited properties when needed.
ownProp: own