In his Eloquent Javascript, Haverbeke claims that (page 16):
\"In a JavaScript system, most of this data is neatly separated into things called va
They're a type of object.
The typeof
is "function"
:
typeof (function() {}) === "function" // true
The internal [[Class]]
is [object Function]
:
({}).toString.call(function() {}) === "[object Function]" // true
They're an instance of the Function
constructor prototype:
(function(){}) instanceof Function // true
They're an instance of the Object
constructor prototype:
(function(){}) instanceof Object // true
You need to be careful when talking about types in javascript. Values have a Type, which may be one of the following:
Perversely, the value returned by the typeof operator is not the Type, it's a string that is the same as the Type for most values, but is different for:
[[Call]]
returns function, even though its Type is ObjectSo the bottom line is that the Type of a function is Object, but typeof someFn
returns function
.
JavaScript supports functional programming. As a result all JavaScript functions are first-class functions, meaning that functions are treated like ordinary objects.
http://en.wikipedia.org/wiki/First-class_functions