In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "the relationship between JS constructor and instantiation and the introduction of prototype example analysis". In daily operation, it is believed that many people have doubts about the relationship between JS constructor and instantiation and the introduction of prototype example analysis. The editor consulted all kinds of data and sorted out simple and useful operation methods. I hope it will be helpful to answer the question of "the relationship between JS constructor and instantiation and the introduction of example analysis of prototype"! Next, please follow the editor to study!
1. Constructor and instantiation
When our programming is object-oriented, first the abstract process = > and then the instantiated process, for example, we abstract a person, I know a person's basic information. Name, age, sex,.... Wait
We start with abstraction, and after the abstraction is complete, we are instantiating.
2. The relationship between constructor and instantiation? / / this custom constructor instantiates function Person (name,age,sex) {this.name=name; this.age=age; this.sex=sex; this.say=function () {console.log ("my name", name)}} / / this process instantiates let per1=new Person ('Sito', 300 'female'); per1.say (); / / calls / / let per1=new Person ('Sito', 300 'female')
Through the above line of code.
We can draw a conclusion:
The relationship between the constructor and the instance object is:
The instance object needs to be created through the constructor.
At the same time: we can know that the constructor of the instance object is the constructor.
Let's prove whether this sentence is correct; the above code does not change.
Console.log (per1.constructor===Person) / / returns true
Fully explain: it is true that the constructor of the instance object is the constructor.
3. Whether per1.say equals per2.say function Person (name,age,like) {this.name=name; this.age=age; this.like=like; this.say=function () {console.log ('I can skip eating');}} var per1=new Person ('Si Teng', 300 'play'); var per2=new Person ('shallow', '10000'); per1.say (); per2.say () Console.log (per1.say==per2.say) / / false4, per1.say are not equal to the conclusion drawn by per2.say.
Because console.log (per1.say==per2.say) / / false
We can come to a conclusion.
That is, per1.say () and per2.say () do not call the same method
So are their contents equal?
Console.log (per1.say () = = per2.say ()) / / true
It means that the content is equal.
5. Example code problems function Person (name,age,like) {this.name=name; this.age=age; this.like=like; this.say=function () {console.log ('I can skip eating');}}; for (var index = 0; index < 100; index++) {var per=new Person ('play'); per.say ();}
This code: it opens up 100 spaces in memory. Each space has a say method. But each say method is different. But their output is the same. Or the logic of execution is the same. This results in a waste of space. So in the project, this results in a waste of space.
Can we optimize it?
5.2 optimize the code solution resulting in space waste function comSay () {/ / execute the same logic console.log ('I don't have to eat')}; function Person (name,age,like) {this.name=name; this.age=age; this.like=like; this.say=comSay;// without parentheses}; var per1=new Person ('Sito', 300 'play'); var per2=new Person ('shallow', '10000') Console.log (per1.say==per2.say) / / true
In this way, we save space. Each time it is called, it is the same method.
Using this method, we can also use the prototype method function Person (name,age,like) {this.name=name; this.age=age; this.like=like;}; Person.prototype.comSay=function () {console.log ('I can skip eating')} var per1=new Person ('Sito', 300 'play'); var per2=new Person ('shallow', '10000') Console.log (per1.comSay==per2.comSay) / / true// We can also use prototypes to solve data sharing
The function of prototype: data sharing and space saving.
At this point, the study on the relationship between JS constructor and instantiation and the introduction of prototypes into example analysis is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.