1. Constructor and instantiationWhen our programming is object-oriented, the first process is abstraction => then instantiation. For example, we abstract a person and I know the basic information of the person. Name, age, gender, etc. We abstract first, and after the abstraction is completed, we instantiate. 2. What is the relationship between constructor and instantiation?//This custom constructor is in the abstract function Person(name,age,sex){ this.name=name; this.age=age; this.sex=sex; this.say=function(){ console.log("My name is",name) } } // This process is instantiation let per1=new Person('司藤',300,'女'); per1.say(); //Call //let per1=new Person('司藤',300,'女'); Through the above line of code.
console.log( per1.constructor===Person ) //Returns true
3. Is per1.say equal to per2.say?function Person(name,age,like) { this.name=name; this.age=age; this.like=like; this.say=function(){ console.log('I can skip meals'); } } var per1=new Person("司藤",300,'玩'); var per2=new Person('白浅','10000','玩'); per1.say(); per2.say(); console.log( per1.say == per2.say ) //false 4. per1.say is not equal to the conclusion drawn by per2.say
5. Example code problem5.1 Problems with the codefunction Person(name,age,like) { this.name=name; this.age=age; this.like=like; this.say=function(){ console.log('I can skip meals'); } }; for (var index = 0; index < 100; index++) { var per=new Person("司藤",300,'玩'); per.say(); }
Can we optimize it? 5.2 Optimize code to solve space wastefunction comSay(){ // Execute the same logic console.log('I can skip meals') }; function Person(name,age,like) { this.name=name; this.age=age; this.like=like; this.say=comSay; //Do not add brackets}; var per1=new Person("司藤",300,'玩'); var per2=new Person('白浅','10000','玩'); console.log( per1.say==per2.say ) //true This way we save space. Each time it is called, it is the same method. 5.3 Using this method, we can also use the prototype methodfunction Person(name,age,like) { this.name=name; this.age=age; this.like=like; }; Person.prototype.comSay=function(){ console.log('I can skip meals') } var per1=new Person("司藤",300,'玩'); var per2=new Person('白浅','10000','玩'); console.log( per1.comSay==per2.comSay ) //true // We can also solve data sharing through prototypes
This concludes this article on the relationship between JS constructors and instantiations and the introduction of prototypes. For more information on the relationship between JS constructors and instantiations and the introduction of prototypes, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Introduction to the use of this in HTML tags
>>: Detailed examples of how to use the box-shadow property in CSS3
Table of contents Preface 1. Extract data 2. Alia...
Let's start with the body: When viewing a web ...
This article shares the specific code of JavaScri...
1. Background We do some internal training from t...
Nginx is a high-performance website server and re...
concept If the index contains all the data that m...
Code first, then text Copy code The code is as fol...
This article introduces Nginx from compilation an...
Table of contents Implementing state sharing base...
Writing method 1: update sas_order_supply_month_p...
Table of contents 1. for loop: basic and simple 2...
Scenario: When starting tomcat in docker (version...
The async_hooks module is an experimental API off...
Table of contents Preface 1. First completely uni...
Preface In project development, there are many wa...