1

I'm working my way through Douglas Crockford's JavaScript: The Good Parts and I ran into something in Chapter 4, Functions, which I'm not quite grasping.

Near the beginning of the chapter:

Function Objects

Functions in JavaScript are objects. Objects are collections of name/value pairs having a hidden link to a prototype object. Objects produced from object literals are linked to Object.prototype. Function objects are linked to Function.prototype (which is itself linked to Object.prototype).

Now this seemed pretty straightforward until I tried this:

console.log({}.prototype); // => undefined, ???
console.log({key: 'val'}.prototype); // => undefined
console.log(Object.prototype); // => [object Object]
console.log(function(){}.prototype); // => [object Object]
console.log(Function.prototype); // => function Empty() {}
console.log(function(){}.prototype === Function.prototype); // => false
console.log(function(){}.prototype === Object.prototype); // => false

Can somebody interpret this in light of the excerpt? Based on what I read, object literals should have prototypes and they should be linked in some way to Object.prototype. Additionally, shouldn't there be some link between function(){}.prototype and Function.prototype? And what is the link between Function.prototype and Object.prototype?

4
  • 1
    The links are there, but it's not really anything you can access. It happens "behind the scenes" with no exposure (technical word) through the API. There may be some circumstances where it's good to know all this stuff, but for the most part you don't need to.
    – user2625787
    Sep 8, 2013 at 22:34
  • Thanks for that. I'm trying to get a handle on all this prototype chain stuff and those results were just confusing me. Chapter 5 should shed some light on it, hopefully.
    – t.888
    Sep 8, 2013 at 23:40
  • I was wondering the same and doing the same kind of tests. This is also mentioned (where I get confused) in the chapter 3 "Objects" in the Prototype section. "All objects created from object literals are linked to Object.prototype"
    – asumaran
    Oct 6, 2013 at 0:39
  • possible duplicate of How does JavaScript .prototype work?
    – Etheryte
    Feb 20, 2015 at 1:52

0

Your Answer

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

Browse other questions tagged or ask your own question.