当前位置:网站首页>Relationship between prototype, prototype chain, constructor and instance
Relationship between prototype, prototype chain, constructor and instance
2022-07-21 20:37:00 【Lotus awn】
As shown in the figure :
1.instanceof Detect the relationship between constructor and instance :
function Person () {.........}
person = new Person ()
res = person instanceof Person
res // true
2. The instance inherits the defined properties on the prototype :
function Person () {........}
Person.prototype.type = 'object n'
person = new Person ()
res = person.type
res // object n
3. Instance access ===> Prototype
Case passed __proto__ Visit the prototype person.proto=== Person.prototype
4. Prototype access ===> Constructors
Prototypes pass constructor Property access constructor Person.prototype.constructor === Person
5. Instance access ===> Constructors
person.proto.constructor === Person
Two 、 Prototype chain
In the process of reading the properties of an instance , If the attribute is not found in this instance , Then it will follow proto Search for the specified prototype , If you can't find , Then look for the prototype of the prototype :
1. Find on instance
function Person() {}
Person.prototype.type = "object name Person";
person = new Person();
person.type = " I am the own attribute of the instance ";
res = Reflect.ownKeys(person); // Try to get your own attributes
console.log(res);
res = person.type;
console.log(res); // I am the own attribute of the instance ( Search up through the prototype chain and give priority to the search in the instance )
2. Look for on the prototype
function Person() {}
Person.prototype.type = "object name Person";
person = new Person();
res = Reflect.ownKeys(person); // Try to get your own attributes
console.log(res);
res = person.type;
console.log(res); //object name Person
3. Look for the prototype of the prototype
function Person() {}
Person.prototype.type = "object name Person";
function Child() {}
Child.prototype = new Person();
p = new Child();
res = [p instanceof Object, p instanceof Person, p instanceof Child];
console.log(res); //[true, true, true] p Belong to at the same time Object,Person, Child
res = p.type; // Layer by layer search
console.log(res); //object name Person ( Search on the prototype chain )
console.dir(Person);
console.dir(Child);
4. Search on the prototype chain
Prototypes can also be passed proto Access the prototype of the prototype , For example, here is a constructor Child then “ Inherit ” The former has a constructor Person, then new Child Get an example p;
When accessing p When a non self owned attribute in , Would pass proto As a series of prototypes connected by bridges 、 The prototype of the prototype 、 Prototype prototype prototype until Object So far ;
Prototype chain search found null until , If you can't find the accessed attribute, stop :
function Person() {}
Person.prototype.type = "object name Person";
function Child() {}
Child.prototype = new Person();
p = new Child();
res = p.__proto__;
console.log(res); //Person {}
res = p.__proto__.__proto__;
console.log(res); //Person {type:'object name Person'}
res = p.__proto__.__proto__.__proto__;
console.log(res); //{.....}
res = p.__proto__.__proto__.__proto__.__proto__;
console.log(res); //null
边栏推荐
- Image upload
- [HTTP cache]
- Small game items - mine sweeping
- How to view your own edition of eigen
- Click the back button to return to the previous page
- [c language] selecting the loop can also play the code like this to show the country of light
- In depth analysis - structure
- Data analysis of akali teaching Bureau in the first half of the fifth set
- 自定义MVC原理
- Fifth Bureau akali teaching Bureau data analysis
猜你喜欢
随机推荐
Custom MVC principle
moment自定义时间区间
SimpleDateFormat正确使用
不拉扯了 不拉扯了 碎碎念 下
C语言实现三子棋小游戏---格局打开版(
(剑指offer版)字符串逆置
How to get the second child element under the first child element
Goodbye, everyone (although no one will notice)
无人机的微分平坦性详细推导
vant toast 内容一直被第一次触发的覆盖
Upload pictures and avatars
第五局 阿卡丽教学局 上半场数据分析
VS,MATLAB,leetcode注释快捷键
Detailed derivation of Differential Flatness of UAV
自定义MVC增删改查
自学编程80余年,这些私藏的实用工具&学习网站陪我走到了现在,必须收藏,学习效率翻倍 - 网站篇
MySQL之CRUD
Jump and receive parameters of JS page
第四局 匹配 上 数据分析
Simpledateformat is used correctly