In JavaScript, each function has a prototype (prototype) attribute, which is an object. Its function is to enable all object instances of a specific type to share the properties and methods it contains.
The prototype is a very special object in JavaScript. When a function is created, a prototype object will be generated. When a specific object is created through the constructor of this function, in this specific object , there will be a property pointing to the prototype.
The following code demonstrates how to create JavaScript objects through prototypes. Using prototype-based creation, the properties and methods can be set as properties and methods exclusive to the object Person. They can no longer be called through the window, thus meeting the encapsulation requirements of the object.
function Person(){}; Person.prototype.name = "Leon"; Person.prototype.age = 22; Person.prototype.say = fucntion(){ alert(this.name + "," + this.age); } var p1 = new Person(); p1.say();
Memory model analysis of prototype
In the process of using the prototype method to create a class, the prototype will have 4 different states in the memory. We still use the above example of creating the Person class to analyze the prototype memory model. The code is as follows:
// 第一种状态 function Person(){}; // 第二种状态 Person.prototype.name = "Leon"; Person.prototype.age = 22; Person.prototype.say = fucntion(){ alert(this.name + "," + this.age); } // 第三种状态,创建了一个对象之后会有一个_proto_属性指向原型 // 在使用时如果对象内部没有找到该属性,就会去原型中查找 var p1 = new Person(); p1.say(); // 第四种状态 var p2 = new Person(); p2.name = "Ada"; p2.say();
First, we create a Person function through function Person(){};. At this time, the prototype memory model of the Person function is as shown below:
// 第一种状态 function Person(){};
A space will be allocated for the Person function in the memory, and there will be a prototype attribute in this space. In addition, a prototype object will be created for the function, and there will be a constructor attribute in the prototype object. The relationship between the Person function and its prototype object is shown in the figure. We call the memory model at this time the first state of the prototype.
Then we set the properties and methods for the Person function through the prototype, which is the second state of the prototype memory model.
// 第二种状态 Person.prototype.name = "Leon"; Person.prototype.age = 22; Person.prototype.say = fucntion(){ alert(this.name + "," + this.age); }
After adding the above code, the memory model structure of the prototype is as shown below:
At this time, the methods and attributes added through the prototype are all Stored in the prototype's memory space.
Next, after we complete the creation of the Person class, we can create a Person object through the new keyword. This is the third state of the prototype memory model.
// 第三种状态 var p1 = new Person(); p1.say();
The third state of the prototype memory model is shown in the figure below:
We create a Person object p1 through new Person(). At this time A memory space will be allocated for the p1 object in the memory. There will be a _proto_ internal attribute in the memory space of p1. This internal attribute cannot be accessed and it also points to the Person prototype.
Although _proto_ internal attributes are hidden, we can use the Person.prototype.isPrototypeOf(p1) method to detect whether p1 has _proto_ pointing to the prototype of Person.
console.info(Person.prototype.isPrototypeOf(p1)); // 控制台输出:true
After completing the creation of the p1 object, the say() method is called through the p1 object. At this time, there is no say() method in the memory space of the p1 object. When the say() method cannot be found in the memory space of p1, JavaScript will search the prototype of Preson through the _proto_ attribute, and after finding it, the corresponding say() method will be executed.
In addition to the above three states, the prototype memory model also has a fourth state.
If we create another Person object p2 and modify the name attribute of the p2 object to "Ada", the fourth state of the prototype memory model will appear.
// 第四种状态var p2 = new Person();p2.name = "Ada";p2.say();
The prototype memory model after calling the above code is as shown below:
When the object p2 is created, it will also be stored in the memory for it. Allocate space, and there will also be a _proto_ internal attribute in the space of the p2 object pointing to the prototype of Person.
When we assign a value to the name attribute of object p2 through p2.name = "Ada";, JavaScript will set its own name attribute in the memory space of p2 and set the value to "Ada".
Then we called the say() method. In this method, we need to obtain the name attribute of p2. It will first search whether there is a name attribute in the memory space of the p2 object itself. If it is found, it will not Then go to the Person prototype to find it. Obviously, there is a name attribute in the space of the p2 object at this time, so the name printed by calling the say() method is "Ada", not "Leon".
It is important to note that the value in the prototype will not be replaced, but will only be overwritten by the same-named property in the object's own space during property search.
The above are the four states of the prototype memory model. Understanding these four states is the key to mastering the prototype.
The above is the content of JavaScript object-oriented prototype memory model. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools