5

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

4
  • Every object has already prototype od Object in it, that's why you can do ({ a: 3 }).toString() for example Commented Aug 8, 2022 at 7:11
  • 2
    @KonradLinkowski Well, not every object. Object.create(null).toString() won't work. (Just nitpicking.) Commented Aug 8, 2022 at 8:04
  • @FZs yes, js is known for misimplementing Java features. In Java null.toString() would work. They made typeof null === 'object' in JavaScript, but didn't really make it an object. Commented Aug 8, 2022 at 8:33
  • 1
    @KonradLinkowski That wasn't my point, but you're right. (typeof null being 'object' is actually because of a kind-of bug in the first version of JS, and they standardized it because people relied on it by then. End of tangent.) I just wanted to say that not every object has Object.prototype in its prototype chain, even though most of them does. Commented Aug 8, 2022 at 8:52

4 Answers 4

1

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 (i.e. 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.

Sign up to request clarification or add additional context in comments.

Comments

1

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%).

Comments

0

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 i.e this = {}.

// 3. the empty object is linked to the prototype of the constructor functio i.e {}.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!

2 Comments

"When it comes to javascript every thing is an object." no, primitives are not objects. There are object wrappers for primitives but the very fact wrappers exist suggests that not everything is an object.
Yes your actually right there are primitive and object data type, but that's not the point i'm trying to make here i'm just trying to explain what i understand about the prototypal inheritance to ma dude. so rather than taking my answer out of context why don't you provide him with a better explanation T!
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.