Detailed analysis of JavaScript prototypes and prototype chains
JavaScriptThere is no concept of classes, but almost everything is based on objects, and inheritance can also be achieved. This is js The biggest difference from other OOP languages is that this is also the most difficult part of js to understand. Let me talk about my personal understanding below.
Let’s start with creating an object. Generally, there are the following methods:
1. Create an Object instance and then add it to it Properties and methods.
var person() = new Object(); person.name = 'mikej'; person.sayName = function(){ alert(this.name); }
2. You can also write like this:
var parson = { name : 'mikej', sayName : function(){ alert(this.name); } }
3. These two methods of creating objects are very simple, but both have flaws. When using the same mode to create objects, a large number of Duplicate code. So there is Factory Pattern:
function createPerson(name){ var p = new Object(); p.name=name; p.sayName = function(){ alert(this.name); }; return p; } var p1 = createPerson('mikej'); var p2 = createPerson('tom');
This way you can create unlimited objects.
4. There is another method, which is similar to the factory mode, called ConstructorMode:
function Person(name){ this.name=name this.sayName = function(){ alert(this.name); } this.say = function(){ alert('hi'); } } var p1 = new Person('mikej'); var p2 = new Person('tom');
There are several things worth paying attention to here: no displayed created objects . The function name Person uses the capital letter P (this is required). Both p1 and p2 have a constructor attribute pointing to Person. At the same time, p1 and p2 are both instances of Object and instances of Person.
alert(p1.constructor == Person); //true alert(p1 instanceof Object); //true alert(p1 instanceof Person); //true
//5.11Update: From a phper perspective, it was easy to think of the process of creating objects like this. Person is a "class", and then use new Person('mikej')
instantiates this class and passes in parameters. But this is not actually the case. The creation process should be like this: First, create an empty object, and then use the apply method. The first parameter is the empty object, and the second parameter is the context parameter, so that this in Person It will point to this object, which is p1.
var p1 = new Person('mikej'); //上面代码就相当于 var p1 = {}; Person.apply(p1, ['mikej']);
The constructor pattern looks good, but one of its drawbacks is that it wastes memory. Continue with the example
alert(p1.say == p2.say) //false
. In order to avoid this defect, use Prototype pattern To create an object, each object in js has a Detailed analysis of JavaScript prototypes and prototype chains attribute that points to another object. All properties and methods of this object will be inherited by the instance of the constructor and are shared. This means that we can put those Immutable properties and methods are defined on the Detailed analysis of JavaScript prototypes and prototype chains object.
function Person(name){ this.name = name; } //Person的原型对象 Person.Detailed analysis of JavaScript prototypes and prototype chains = { say: function(){ alert('hi'); }, sayName: function(){ alert(this.name); } }; var p1 = new Person("mikej"); var p2 = new Person("tom"); p1.sayName(); p2.sayName(); //下面就可以看出方法实现了共享 alert(P1.say == P2.say) //true alert(P1.sayName == P2.sayName) //true
Let’s expand the above example and use Detailed analysis of JavaScript prototypes and prototype chainss to implement inheritance.
function Person(name){ this.name = name; } Person.Detailed analysis of JavaScript prototypes and prototype chains = { say: function(){ alert('hi'); }, sayName: function(){ alert(this.name); } }; function Programmer(){ this.say = function(){ alert('im Programmer, my name is ' + this.name); } } Programmer.Detailed analysis of JavaScript prototypes and prototype chains = new Person('mikej'); //手动修正构造函数 Programmer.Detailed analysis of JavaScript prototypes and prototype chains.constructor = Programmer; var p1 = new Programmer(); console.dir(Programmer.Detailed analysis of JavaScript prototypes and prototype chains.constructor);//Programmer console.dir(p1.constructor);//Programmer console.dir(p1);
Programmer’s Detailed analysis of JavaScript prototypes and prototype chains points to an instance of Person, then all Programmer instances can inherit Person and Person’s Detailed analysis of JavaScript prototypes and prototype chains.
There will be a problem here.
The default Detailed analysis of JavaScript prototypes and prototype chains object has a constructor attribute pointing to its constructor. Each instance also has a constructor attribute, which will call the constructor attribute of the Detailed analysis of JavaScript prototypes and prototype chains object by default.
Assume there is no Programmer.Detailed analysis of JavaScript prototypes and prototype chains = new Person('mikej');
Programmer.Detailed analysis of JavaScript prototypes and prototype chains.constructor points to Programmer. The construction of p1 also points to Programmer
alert(Programmer.Detailed analysis of JavaScript prototypes and prototype chains.constructor == Programmer) //true alert(p1.constructor == Programmer) //true
But with this sentenceProgrammer.Detailed analysis of JavaScript prototypes and prototype chains = new Person('mikej');
After that,
Programmer.Detailed analysis of JavaScript prototypes and prototype chains.constructor Points to Object, which is the construction of the object pointed to by Person.Detailed analysis of JavaScript prototypes and prototype chains. p1.constructor also points to Object. But p1 is obviously generated by the constructor Programmer, which causes inheritance confusion, so we must manually correct the constructor, which is the code below.
Programmer.Detailed analysis of JavaScript prototypes and prototype chains.constructor = Programmer;
Okay, now let’s take a look at the Detailed analysis of JavaScript prototypes and prototype chains chain:
console.dir(p1);
The result of this code is
It can be seen that, p1 is an instance of Programmer. The Detailed analysis of JavaScript prototypes and prototype chains of Programmer is Person, and the Detailed analysis of JavaScript prototypes and prototype chains of Person is Object. On the Internet, it is a JS object. This is the Detailed analysis of JavaScript prototypes and prototype chains chain, that is to say, JavaScript inheritance is implemented based on the Detailed analysis of JavaScript prototypes and prototype chains chain.
The above is the detailed content of Detailed analysis of JavaScript prototypes and prototype chains. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
