0

Can Anyone tell me what will be the prototype of an simple object when that object is created using Object Literal Notation/ Object.create ? When i went through MDN Notes on Object.create it was stated something like below

var a = {a: 1}; 
// a ---> Object.prototype ---> null

var b = Object.create(a);
// b ---> a ---> Object.prototype ---> null
console.log(b.a); // 1 (inherited)

var c = Object.create(b);
// c ---> b ---> a ---> Object.prototype ---> null

var d = Object.create(null);
// d ---> null
console.log(d.hasOwnProperty); // undefined, because d doesn't inherit from Object.prototype

But Whenever i tried console.log(b.prototype) it gave me undefined, same with the case of "c" and "d" Object. I am confused here, if b's prototype is "a" according to MDN, then why is it giving undefined.

One more question, if b.prototype is undefined, then how come console.log(b.a); is resulting 1. How is it inheriting in this case ?

Razer
  • 39
  • 1
  • 6

2 Answers2

0

To get the prototype of an object, you use x.__proto__ or Object.getPrototypeOf(x) rather than x.prototype. Note that __proto__ is now actually deprecated in favor of Object.getPrototypeOf().

The property you are using, x.prototype is a property only found on functions. It is used to build __proto__ when the function is called as a constructor using new.

Jasvir
  • 516
  • 2
  • 5
0

Prototype is not a member of instances only Function instances have prototype. Or you can add a prototype member but it wouldn't do anything for the prototype chain.

All objects have something called proto (a non standard property called __proto__) that is used to look up members not specified on the instance.

More information can be found here: https://stackoverflow.com/a/16063711/1641941

Community
  • 1
  • 1
HMR
  • 37,593
  • 24
  • 91
  • 160