Home Web Front-end JS Tutorial Detailed explanation of prototype chaining based on JavaScript implementation of inheritance mechanism_javascript skills

Detailed explanation of prototype chaining based on JavaScript implementation of inheritance mechanism_javascript skills

May 16, 2016 pm 05:34 PM
prototype chain

If you redefine the classes in the previous example using prototypes, they will become the following form:

Copy the code The code is as follows :

function ClassA() {
}

ClassA.prototype.color = "blue";
ClassA.prototype.sayColor = function () {
alert(this.color);
};

function ClassB() {
}

ClassB.prototype = new ClassA();


The magic of the prototype approach lies in the last line of code. Here, set the prototype property of ClassB to an instance of ClassA. This is interesting because you want all the properties and methods of ClassA, but you don't want to add them one by one to the prototype property of ClassB. Is there a better way than assigning an instance of ClassA to the prototype property?

Note: Call the constructor of ClassA without passing parameters to it. This is standard practice in prototype chains. Make sure the constructor doesn't have any parameters.

Similar to object impersonation, all properties and methods of the subclass must appear after the prototype property is assigned, because all methods assigned before it will be deleted. Why? Because the prototype property is replaced with the new object, the original object with the new method added will be destroyed. Therefore, the code to add the name attribute and sayName() method to the ClassB class is as follows:

Copy the code The code is as follows:

function ClassB() {
}

ClassB.prototype = new ClassA();

ClassB.prototype.name = "";
ClassB.prototype.sayName = function () {
alert(this.name);
};


Yes Test this code by running the following example:
Copy the code The code is as follows:

var objA = new ClassA();
var objB = new ClassB();
objA.color = "blue";
objB.color = "red";
objB.name = "John";
objA.sayColor();
objB.sayColor();
objB.sayName();

In addition, in the prototype chain, the instanceof operator operates also Very unique. instanceof returns true for both ClassA and ClassB for all instances of ClassB. For example:
Copy code The code is as follows:

var objB = new ClassB();
alert(objB instanceof ClassA); //Output "true"
alert(objB instanceof ClassB); //Output "true"

In the weakly typed world of ECMAScript, this is extremely Useful tool, but cannot be used when using object impersonation. However, since the prototype of the subclass is directly reassigned, the following situation occurs:
Copy code The code is as follows:

console.log(objB.__proto__===objB.constructor.prototype) //false

Because the prototype attribute of ClassB's prototype chain has been overridden by an object of another class . The output results show that objB.__proto__ still points to ClassB.prototype, not objB.constructor.prototype. This is also easy to understand. What is assigned to Person.prototype is an object literal new ClassA() instance. The constructor (constructor) of an object defined using the object literal method points to the root constructor Object. Object.prototype is a Empty object {}, {} is naturally different from ClassB.prototype.
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are prototypes and prototype chains What are prototypes and prototype chains Nov 09, 2023 pm 05:59 PM

Prototype, an object in js, is used to define the properties and methods of other objects. Each constructor has a prototype attribute. This attribute is a pointer pointing to a prototype object. When a new object is created, the new object will be The prototype attribute of its constructor inherits properties and methods. Prototype chain, when trying to access the properties of an object, js will first check whether the object has this property. If not, then js will turn to the prototype of the object. If the prototype object does not have this property, it will continue to look for the prototype of the prototype.

What is scope chain and prototype chain? What is scope chain and prototype chain? Nov 13, 2023 pm 01:46 PM

Scope chain and prototype chain are two important concepts in JavaScript, corresponding to the two core features of scope and inheritance respectively: 1. Scope chain is a mechanism used to manage variable access and scope in JavaScript. It is formed by It is determined by the execution context and lexical scope in which the function is created; 2. The prototype chain is a mechanism for implementing inheritance in JavaScript. Based on the prototype relationship between objects, when accessing the properties or methods of an object, if the object itself does not Definition, will be searched up along the prototype chain.

What is the difference between prototype and prototype chain What is the difference between prototype and prototype chain Nov 09, 2023 pm 04:48 PM

The difference between prototype and prototype chain is: 1. Prototype is an attribute that each object has, including some shared attributes and methods, which is used to realize the sharing and inheritance of attributes and methods between objects, while prototype chain is a The inheritance mechanism is implemented through the prototype relationship between objects, which defines the inheritance relationship between objects so that objects can share the properties and methods of the prototype object; 2. The function of the prototype is to define the shared properties and methods of the object, so that multiple Objects can share the properties and methods of the same prototype object, and the function of the prototype chain is to realize the inheritance relationship between objects, etc.

What is the purpose of prototypes and prototype chains? What is the purpose of prototypes and prototype chains? Jan 13, 2024 pm 12:58 PM

The reason why prototypes and prototype chains exist is to implement inheritance and sharing of object properties in the JavaScript language. In JavaScript, everything is an object, including functions. Every object has a property called a prototype that points to another object, which is called a prototype object. Objects can inherit properties and methods from prototype objects. The benefit of implementing shared properties and methods through prototypes is memory savings. Consider an object A, which has some properties and methods, then create object B and make

What is the prototype chain in es6 What is the prototype chain in es6 Nov 15, 2022 pm 07:28 PM

Prototype chain, simply understood is a chain composed of prototypes. When accessing an attribute of an object, it will first search on the attribute of the object itself. If it is not found, it will search on its __proto__ implicit prototype, that is, the prototype of its constructor. If it has not been found yet, It will then search in the __proto__ of the prototype of the constructor. In this way, searching upward layer by layer will form a chain structure, which is called a prototype chain.

In-depth discussion: Analysis of the role of prototypes and prototype chains in object-oriented programming In-depth discussion: Analysis of the role of prototypes and prototype chains in object-oriented programming Jan 11, 2024 am 11:59 AM

In-depth analysis: The role of prototype and prototype chain in object-oriented programming requires specific code examples. In object-oriented programming (OOP), prototype (Prototype) and prototype chain (PrototypeChain) are important concepts. They provide an object-based code reuse mechanism and play a key role in languages ​​such as Javascript. In this article, we'll take a deep dive into the concepts of prototypes and prototype chains, explore their role in OOP, and illustrate with concrete code examples

What is the role of js prototype and prototype chain What is the role of js prototype and prototype chain Nov 09, 2023 pm 04:56 PM

The function of js prototype and prototype chain is to realize the inheritance of objects, save memory space, and improve the performance and maintainability of the code. Detailed introduction: 1. Implement the inheritance of objects. The prototype and prototype chain allow you to create an object and inherit the properties and methods of another object. When you create a new object, you can point its prototype to another object, so that the new object The object can access the properties and methods on the prototype object; 2. Save memory and improve performance. In JavaScript, each object has a prototype. Through the prototype chain, objects can share prototypes, etc.

Explore the peculiarities of prototypes and prototype chains Explore the peculiarities of prototypes and prototype chains Jan 13, 2024 pm 03:50 PM

Exploring the unique features of prototype and prototype chain In JavaScript, prototype and prototype chain are very important concepts. Understanding the unique features of prototypes and prototype chains can help us better understand inheritance and object creation in JavaScript. A prototype is a property owned by every object in JavaScript that points to another object and is used to share properties and methods. Every JavaScript object has a prototype

See all articles