Home > front end >  The prototype chain
The prototype chain

Time:04-13

Redirect the prototype chain, zha result different?

 let p={}; 
P. __proto__=Array. The prototype;
P. lice.//? Slice () {} [native code], the results of normal


Zha so that it no good?
 let o=Object. The create (null). 
O. prototype; __proto__=Array.
Grown lice.//undefined.

CodePudding user response:

{} with the Object. The create (null) created the Object is not the same, the latter is no attributes,

CodePudding user response:

Not even [[prototype]],,,

CodePudding user response:

refer to the second floor FnaticE response:
didn't even [[prototype]],,,

No, should be a [[prototype]] is null,

CodePudding user response:

Let o=Object. The create (null);
You specify the o the prototype of the object is null, you can be as o inherited the null, the problem is null what all have no, do not have any attributes and methods, on the prototype chain can't find anything, so o do so, that is why you've grown lice undefined reason, because you are really didn't give it a definition, just o. __proto__=Array. The prototype; Let you illusion, think o inherited Array, but actually, o. __proto__ and you define o.a onychoteuthis, o. chua didn't have any different, but you just give a property name call: __proto__

Object. The create (null) is the most clean a null Object, clean to what all have no, is a piece of blank paper, you want to draw what painting what, all properties and methods defined by yourself, don't have to worry about covering on the prototype chain method, because it is what all have no, enough freedom is bold and unrestrained, it is simply streaking

The
 let p={}; 

Equivalent to the following, inherited object
 let p=Object. The create (Object. The prototype) 



 
Let o=Object. The create (null)
O. prototype __proto__=Array.

Const arr1=new o. __proto__. Constructor ()

The console. The log (o. __proto__===[] __proto__)//=& gt; True
The console. The log (o. constructor===__proto__. [] __proto__. The constructor)//=& gt; True
The console. The log (arr1. __proto__. Constructor===[] __proto__. The constructor)//=& gt; True
The console. The log (o. __proto__. Slice===[] __proto__. Slice)//=& gt; True

Arr1. Push (1)
The console. The log (arr1)//=& gt; [1]

//if the above allow you to produce the illusion, think o inherited Array
//here is disproof: only you just add an attribute name call: to o __proto__
//that's why you've grown lice undefined reasons, because o originally what properties and methods all have no, you didn't give it a definition slice method, so it does not define

Let o1=Object. The create (null)
O1. Prop=Array. The prototype

Const arr2=new o1. Prop. Constructor ()

The console. The log (o1) prop===[]) __proto__)//=& gt; True
The console. The log (o1. Prop. Constructor===[] __proto__. The constructor)//=& gt; True
The console. The log (arr2. __proto__. Constructor===[] __proto__. The constructor)//=& gt; True
The console. The log (o1. Prop. Slice===[] __proto__. Slice)//=& gt; True

Arr2. Push (1)
The console. The log (arr2)//=& gt; [1]


  • Related