filmov
tv
Prototype in JavaScript - #27 @Everyday-Be-Coding

Показать описание
In JavaScript, prototypes are a fundamental concept that forms the basis of the language's object-oriented programming model. Understanding prototypes is crucial for working effectively with JavaScript's object system. Here's a detailed explanation:
1. Prototype Chain:
JavaScript is a prototype-based language, which means that objects can inherit properties and methods directly from other objects. This inheritance is established through a mechanism called the prototype chain. Each object has an internal property known as [[Prototype]] (commonly referred to as the prototype), which points to another object. When you access a property or method on an object, JavaScript first checks if the object itself has that property or method. If it doesn't find it, it looks at the object's prototype, and if the prototype doesn't have it, it looks at the prototype's prototype, and so on, forming a chain until it reaches the end or finds the property/method.
2. prototype Property:
In JavaScript, constructor functions have a property called prototype. This property is not the prototype of the constructor function itself, but rather the prototype that will be assigned to instances created with that constructor function using the new keyword. You can attach properties and methods to this prototype object, and they will be inherited by all instances created from the constructor function.
Example:
JavaScript code
function Person(name) {
}
};
var person1 = new Person("Alice");
var person2 = new Person("Bob");
Example:
JavaScript code
var personProto = {
sayHello: function() {
}
};
4. __proto__ Property:
While not recommended for direct use due to being non-standard (though widely supported), the __proto__ property provides direct access to an object's prototype. It can be used to get or set the prototype of an object.
Example:
JavaScript code
var cat = {
sound: "Meow"
};
var kitten = {
color: "White"
};
kitten.__proto__ = cat;
Example:
JavaScript code
var person = {
name: "David"
};
var personProto = {
sayHello: function() {
}
};
Understanding prototypes is essential for effectively utilizing JavaScript's object-oriented features and for mastering advanced concepts like inheritance and polymorphism in the language.
Chapter :
00:00 Inro
00:11 Prototype Chain
01:15 Example
01:31 Object Create()
01:57 Proto property
02:20 ObjectGetProtoTypeOf & ObjectSetProtoTypeOf
02:51 Summery
Thank you for watching this video
EVERYDAY BE CODING