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?

2
  • Functions are Objects too :) Commented Apr 21, 2015 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. Commented Apr 21, 2015 at 11:59

1 Answer 1

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());

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

4 Comments

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 :)
Check the second part of my answer. :)
@gerryino Added something for you.
@gerryino: Notice that Function is indeed derived from Function. Only JSON and Math are no functions.

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.