0

I create one object using Object.create method, so Object.create expects protoptype object as the first parameter and property decriptors are the second parameter.

var obj = Object.create({a:2},{b:{value:3}})

so, {a:2} is the prototype of the obj. but if I am looking for prototype like obj.prototype returns undefined but if i checks using Object.getPrototypeOf(obj) returning {a:2}

Can you tell me whats wrong here? So, what is the first parameter in Object.create?

Thanks

1 Answers1

1

You've found the one thing weirdest about the JavaScript language: the prototype property. In JavaScript, objects inherit properties from parent through the prototype chain.

But JavaScript works so that the prototype property of an object is not that object's prototype - it is the value that is used as the prototype for further objects created from it using new. So Car.prototype is the value that's going to be the prototype of creating a new instance with new Car().

What actually is the prototype of an object in the prototypal inheritance sense is what you can get with Object.getPrototypeOf(). You can learn more about this for example in Understanding the prototype property in JavaScript

pspi
  • 11,189
  • 1
  • 20
  • 18