2

I can access the prototype object of a javascript function using the .prototype but when I cannot use .prototype on a javascript object literal. Please let me know why is this behavior inconsistent.

var add = function (a, b) {     return a + b; };
var s={name:'Pradeep'}
console.log('Function\'s prototype >> '+add.prototype)
console.log('Object\'s prototype >> '+ s.prototype)

http://jsfiddle.net/prashdeep/b5xhx80g/

zilcuanu
  • 3,451
  • 8
  • 52
  • 105

1 Answers1

1

The prototype property of a function is not the same thing as the function's prototype (inherited) methods.

myFunction.prototype is the object that will be used as the prototype of objects created using myFunction as a constructor (new myFunction()). See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new

What you're looking for is the __proto__ property, which accesses the inherited (prototype) methods of an object.

Scimonster
  • 32,893
  • 9
  • 77
  • 89
  • This is what I was looking for. Using the __proto__ we can see the object's prototype. – zilcuanu Feb 06 '15 at 09:16
  • The [*language specification*](http://ecma-international.org/ecma-262/5.1/#sec-8.6.2) uses `[[Prototype]]` to refer to the internal prototype property of objects that references the object's constructor's public *prototype*. – RobG Feb 06 '15 at 09:38