1

Just asking why

typeof Number 

provides function as a result.

Other built-in objects like Math or JSON are objects and, according to this answer ( What does the built in object hierarchy look like in javascript? ), they should all be related to Object, not to Function.

Javascript design flaw or there's a meaning in that? Is that answer correct?

Community
  • 1
  • 1
gerryino
  • 380
  • 2
  • 7
  • Functions are Objects too :) – doldt Apr 21 '15 at 11:51
  • Of course, but the real question is why *String* Inherits from *Function* and *JSON* Inherits from *Object*. I can see no sense in this. For example if you add a method to the Function prototype, it will be available to String but not to JSON. – gerryino Apr 21 '15 at 11:59

1 Answers1

1

The Function constructor creates a new Function object. In JavaScript every function is actually a Function object.

Functions are glorified objects.

https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Function

Of course, but the real question is why String Inherits from Function and JSON Inherits from Object. I can see no sense in this. For example if you add a method to the Function prototype, it will be available to String but not to JSON

A JavaScript object is a mapping between keys and values. Keys are strings and values can be anything. This makes objects a natural fit for hashmaps.

Functions are regular objects with the additional capability of being callable.

You can instantiate a String but you cannot instantiate a Math object. This is maybe the fact confusing you.

alert(new String());
alert(new Math());
OddDev
  • 3,644
  • 5
  • 30
  • 53
  • 1
    That's clear, but why *Function*, *JSON* or *Math* built-ins are derived from *Object* but *String* and *Number* are instead derived form *Function* (which in turns Inherits from *Object*)? This is what i don't understand. But maybe there is no answer to this question :) – gerryino Apr 21 '15 at 12:14
  • Check the second part of my answer. :) – OddDev Apr 21 '15 at 12:18
  • @gerryino Added something for you. – OddDev Apr 21 '15 at 12:22
  • @gerryino: Notice that `Function` is indeed derived from `Function`. Only `JSON` and `Math` are no functions. – Bergi Apr 21 '15 at 12:26