In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "what is the relationship between JS constructor instance object and prototype object". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
/ / Custom constructor function Person (name,age,gender) {this.name = name; this.age = age; this.gender = gender; this.eat = function () {console.log ("eat garlic mixed with stinky tofu and durian sauce") };}
Constructor-> create object per
Var per = new Person ('Linda',23,'female')
Add: console.dir (per) can print out the structure of the object per (attributes and attribute values)
We can conclude that the relationship between the instance object and the constructor is as follows:
1. The instance object is created through the constructor-the process of creation is called instantiation
two。 How to determine whether an object is the result of a constructor instantiation? (the second is recommended)
1) through the way of constructor, that is, instance object. Constructor = = Constructor name: console.log (dog.constructor==Animal)
2) instance object instanceof constructor name: console.log (dog instanceof Animal)
Constructors can cause problems in creating objects. Let's take a look at the following example
Function Person (name, age) {this.name = name; this.age = age; this.eat = function () {console.log ("eat braised potatoes today");}} var per1 = new Person ("Xiaobai", 20); var per2 = new Person ("Xiao Hei", 30); per1.eat () Per2.eat ()
By printing the result as false, we can conclude that per1,per2 does not share eat methods, so it is easy to infer that object instances created by custom constructors do not save memory space, which leads to prototypes to solve this problem.
Function Person (name,age) {this.name=name; this.age=age;} / / add methods through prototypes to solve data sharing and save memory space Person.prototype.eat=function () {console.log ("eating cold dishes");} Var p1=new Person (Xiao Ming, 20); var p2=new Person (Xiao Hong, 30)
Through the printing results, we find that the two instance objects of p1Powerp2 share the eat method, which realizes data sharing and saves memory space. But when we look at the structure of the instance object, we don't find the eat method, but the object does call the eat method. Why?
Then let's take a look at the structure of the constructor Person
When we look at the structure of an object through console.dir (p1), we find that in addition to the age,name attribute, there is also a _ proto_ property whose value is an object, which is called an implicit prototype in JavaScript. The implicit prototype of an object points to the prototype prototype that constructs the constructor of the object, which also ensures that the instance can access the properties and methods defined in the constructor prototype.
That's all for "what is the relationship between the JS constructor instance object and the prototype object". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.