简体   繁体   中英

what does Array.Call(o) do with prototypical inheritance?

I read somewhere that :

var o = new Array();

is in effect doing the following:

var o = {};
o.__proto__ = Array.prototype;
Array.call(o)

My question is what is Array.call(o) doing?

If I had o2 = {};

what does Array.call(o2) do?

TIA JD

The new operator in JavaScript has three essential tasks. First, it creates a new empty object. Next, it sets the new object's proto property to match the prototype property of the function being invoked. Finally, the operator invokes the function and passes the new object as the “this” reference

The function here is the array constructor Array() So the first step here is creating an empty object

var o = {};

The second step is setting the proto property

o.__proto__ = Array.prototype;

And then, the final step, you have the invocation of the function, and passing the new object as reference

Array.call(o)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM