简体   繁体   中英

JavaScript Prototype Chain Reference to Object.prototype

I need some help for understanding the prototype chain. I don't understand / not quite sure if my assumptions are correct. I want to understand the link to the Object.prototype. One example

function A(){};
var newObject=new A();

I know the newObject has an internal property [[prototype]] which has a reference to A.prototype. And this prototype-Object has also an internal property with reference to Object.prototype, right? But why does it have that reference? Is it because the prototype of A (used as constructor) is an object which can be imagined to be created by A.prototype=new Object() ( which will be done automatically in the background). And now I have a reference to the prototype of Object? I hope I explained it clearly. Please let me know your comments.

Many thanks

Yes, your understanding is correct.

In JS, pretty much all the objects have a reference to Object.prototype in their prototype chain.

The [[Prototype]] of an object refers to Object.prototype as the end of a prototype chain. In your example, this means what you described:

            [[Prototype]]                  [[Prototype]]                   
newObject -----------------> A.prototype -----------------> Object.prototype

That applies to...

  • objects created by the Object constructor (ie new Object(...) ),
  • those created with an object literal (which you can think of as syntax sugar for a new Object() call followed by a bunch of Object.defineProperty calls) and
  • objects created internally by the JS interpreter (like A.prototype which is created automatically when you define the A function).

There are also exceptions of this "rule" though:

  • Object.prototype itself.

    If it had a reference to... well, itself, it would create and infinite recursion when a nonexistent property is looked up. Hence, it has no prototype , which means that its [[Prototype]] is null (that's how a prototype chain "ends").

  • Custom objects created using Object.create or modified with Object.setPrototype .

    The [[Prototype]] can be set to anything this way, including null or other objects whose prototype chain doesn't end with Object.prototype .

This is indeed what the ECMAScript specification prescribes. When a function is defined with the function keyword, then an object is created from Object.prototype which is assigned to the function's prototype property, and therefor will be used as prototype for any object that is constructed by that function.

This specification can be found at Make Constructor , step 5.a:

5.a. Set prototype to OrdinaryObjectCreate(%Object.prototype%).

When it comes to javascript every value is either an object (arrays, objects, sets, maps...) or a primitive(Numbers, strings, bool...) objects and primitives are the two data types in javascript a. You have the Number, String, Array and even Object constructors which you can use to create the corresponding value. And when you create new object using the new operator the following steps happen consecutively:

// 1. New empty object is created

// 2. the constructor function is called, and the this keyword is assigned to the empty object ie this = {}.

// 3. the empty object is linked to the prototype of the constructor functio ie {}. proto = constructorFunction.prototype.

// 4. the constructor function automatically return the new object {}.

keep in mind that constructorFunction.prototype is the prototype of the new object that is created from the constructorFunction and not of itself.

I hope this clears things up a little for you!

But why does it have that reference?

To answer in one sentence - because prototype is an object itself and has its own prototype. When you create function in JavaScript it has a property called prototype. When you use that function as a constructor function (with new operator), new object is creating which as you wrote is linked to its constructor function prototype via internal [[prorotype]] property. That prototype object is plain JavaScript object which [[prototype]] property references Object.prototype.

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