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/

3
  • 1
    prototype is a property of a function not of obect... Having said that..You may want to do like this.. s.constructor.prototype Commented Feb 6, 2015 at 8:45
  • Possible duplicate of this Commented Feb 6, 2015 at 8:45
  • Thanks Rakesh for your answer Commented Feb 6, 2015 at 9:15

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This is what I was looking for. Using the proto we can see the object's prototype.
The language specification uses [[Prototype]] to refer to the internal prototype property of objects that references the object's constructor's public prototype.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.