Search code examples
javascriptprototypeobject-create

Object.create and Prototypes


When I use object.create to create a new object like so,

o = {x:1,y:2};
p = Object.create(o);

I'm under the impression that o becomes the prototype of p and inherits all its methods.

Then why, when I try

print(p.prototype);

the output is undefined? o is well-defined!!

Thank You


Solution

  • Only functions have a prototype property [ES5].

    p references o through the internal [[Prototype]] property, which was accessible in some browsers with __proto__ [MDN], but is now deprecated.

    You can get a reference to the prototype of an object with Object.getPrototypeOf [MDN]:

    Object.getPrototypeOf(p) === o // true