var foo = function(){alert('foo')}
function bar(){alert('bar')}
Why does foo.prototype point to Object, but the named function bar.prototype points to itself?
I should add that this behavior is observed from browser console.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you’re using a console such a Chrome to log the value of a
prototypeobject then I suggest you don’t. The Chrome console, like other browser consoles, formats the output of the prototype object.So for a named function it will log the
nameof the function, but for an unnamed function it will simply logObject. This does not mean that theprototypeof the function points toObjector the function itself. It’s just that that’s what the console is showing you (someone should sue them for that). See for yourself: http://jsfiddle.net/2xkpC/How does it know the name of a function from it’s
prototype? Well, the prototype of a function has a property calledconstructorwhich points back to the function itself, and the name of the function is stored as a string in a property callednameon the function. So if you log aprototypeobject then it will displayprototype.constructor.name || "Object":See the demo here: http://jsfiddle.net/4bWfn/
If you open your console and click that triangle near the logged output then you’ll see the
constructorand the__proto__properties of the prototype object. Underconstructoryou’ll also see thenameproperty.To know more about inheritance in JavaScript and the
prototypeobject read this answer: https://stackoverflow.com/a/8096017/783743