0

If I create a simple object like

var a = {foo: 1}; 

Why I can access to its prototype this way ?

a.prototype

I get undefined on the console

afdi5
  • 307
  • 2
  • 13

2 Answers2

0

You can find the object's prototype this way:

a.__proto__

obj.prototype can be used to set new prototype to the object.

marsibarsi
  • 973
  • 7
  • 7
0

Functions have a prototype property not regular objects. The prototype property points to an object that child objects created with new will be linked to.

Objects are prototype linked however. However this is not the same thing as the prototype property of functions This naming is a bit of confusing.

For example:

var a = {foo: 1}; 

// the prototype of a
console.log(Object.getPrototypeOf(a))
// the same as __proto__
console.log(a.__proto__ === Object.getPrototypeOf(a))

// the .protoype property:
function Test(){}
// it's just an empty object unless you change it
console.log(Test.prototype)

// instance are prototype linked to this
let instance = new Test()
console.log(instance.__proto__ === Test.prototype)
Mark
  • 90,562
  • 7
  • 108
  • 148