Home Web Front-end JS Tutorial Understanding of several key concepts in JavaScript - construction of prototype chain_javascript skills

Understanding of several key concepts in JavaScript - construction of prototype chain_javascript skills

May 16, 2016 pm 06:06 PM
prototype chain

All functions in Javascript have a prototype attribute, and this prototype attribute is an object type object. All objects constructed by this function have the characteristics of this prototype, which means that the constructed object can be used to directly access the prototype. properties and methods on.
The following code demonstrates how to use prototype:

Copy the code The code is as follows:

function Staff(name) {
this.name = name;
}
Staff.prototype.say = function() {
alert(this.name " say hello");
}
var staff1 = new Staff("hunter");
var staff2 = new Staff("dangjian");
staff1.say();
staff2.say();

Run the above program. It can be seen that the properties and methods on the prototype can be called between created objects. More importantly, the properties and methods in the prototype are shared among objects of the same type
Copy code The code is as follows:
alert( staff1.say == staff2.say);


Another commonly used feature of prototype is to construct the inheritance relationship of objects through prototype. By assigning the base class object to the prototype of the subclass, the inheritance relationship in object-oriented can be simulated. This is what everyone often calls the object-oriented mechanism of JavaScript. . The following code snippet demonstrates the inheritance relationship of constructing objects using this feature:
Copy the code The code is as follows:

function Staff(name) { // Base class
this.name = name;
}
Staff.prototype.say = function() {
alert(this.name " say hello");
}
function ManStaff(name, age) { // Subclass
this.name = name;
this.age = age;
}
ManStaff. prototype = new Staff(); // Establish inheritance relationship
var manStaff1 = new ManStaff("hunter", 22);
var manStaff2 = new ManStaff("dangjian", 32);
manStaff1.say ();
manStaff2.say();

Running the code shows that the ManStaff object has the Say method in the base class Staff. This inheritance method is through the prototype chain in JavaScript. realized. You may be familiar with the above usage of prototype, but as programmers, we not only need to know its usage, we should also understand that it is the internal mechanism of prototype. Let's analyze the principle of prototype and the implementation of prototype chain.
To understand the mechanism of prototype, you must understand how functions are created in JavaScript.
When the code is executed to function Staff(name) {this.name = name;}, it is equivalent to executing var Staff = new Function("name", "this.name = name"). The interpreter will use the predefined Function() constructor to create a function type object, namely Staff.

Then add the __proto__ attribute to the created Staff object and assign it to the prototype of the Function constructor. This step is a step in all object creation processes. When executing something like var x = new X () method is to assign the prototype of X to the __proto__ of :

Copy code

The code is as follows:
var o = new Object(); o.constructor = Base; Staff.prototype = o;
From the above analysis, we can see that when an object is created, a private attribute __proto__ is created, and when a function is created, a prototype attribute is created. Because Staff is a function type object, it will have both properties.
These two properties are key properties for building a prototype chain. Let's analyze how the prototype is passed when executing the code var staff1 = new Staff("hunter").
According to the above analysis, staff1.__proto__ = Staff.prototype, and Staff.prototype is an object created by Object, that is, Staff.prototype.__proto__ = Object.prototype, so staff1.__proto__ .__proto__ points to Object. prototype, that is, staff1.__proto__ .__proto__ == Object.prototype, this is the prototype chain. When you want to read the properties of an object, JS first looks for whether the object itself has this property. If not, it will continue to search along the prototype chain. this property.
If you know the principle of prototype chain, it is easy to build object inheritance in Javascript based on this principle.
From the above analysis, we know that the top of the prototype chain is Object.prototype, which means that Object is the base class of all objects in the built inheritance relationship. You can run the following code verification.
Copy code The code is as follows:

Object.prototype.location = "China";
function Staff(name) { // Base class
this.name = name;
}
Staff.prototype.say = function() {
alert(this.name " say hello") ;
}
var ManStaff1 = new Staff("hunter");
var ManStaff2 = new Staff("dangjian");
alert(ManStaff1.location);
alert(ManStaff2. location);

The running results show that Object is the base class of Staff, so how to build a subclass of Staff?
Understanding the establishment principle of the above function, we can easily write the following code:
Copy the code The code is as follows:

function Staff(name) { // Base class
this.name = name;
}
Staff.prototype.say = function() {
alert(this .name " say hello");
}
function ManStaff(name, age) { // Subclass
Staff.call(this,name);
this.age = age;
}
ManStaff.prototype = new Staff(); // Establish inheritance relationship
var ManStaff1 = new ManStaff("hunter", 22);
var ManStaff2 = new ManStaff("dangjian", 32) ;
ManStaff1.say();
ManStaff2.say();

This is the sentence that establishes the inheritance relationship: ManStaff.prototype = new Staff(); , the inheritance relationship is calculated as follows :ManStaff1.__proto__ = =ManStaff.prototype, ManStaff.prototype.__proto__ = Staff.prototype, Staff.prototype.__proto__ == Object.prototype; then ManStaff1.__proto__.__proto__.__proto__ == Object.prototype.
This inheritance relationship in JavaScript is looser than the traditional object-oriented inheritance relationship, and the construction method is more difficult to understand, but as a scripting language, its functions are already very powerful.
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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 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.

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.

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

What are js prototype and prototype chain What are js prototype and prototype chain Jun 14, 2023 am 11:34 AM

The js prototype and prototype chain are: 1. Prototype. By default, all functions have a public and non-enumerable attribute like "prototype", which points to another object, which is the prototype. 2. Prototype chain. When accessing the properties or methods of an object, the object will first look for it from itself. If it cannot find it, it will look for it in the prototype, that is, in the "prototype" of its constructor. If it cannot be found in the prototype, , even if there is no such attribute in the constructor, it will look for it on the prototype behind the prototype, thus forming a chain structure called a prototype chain.

See all articles