Functions Are Object In Javascript?
var obj = {}; var fn = function(){}; obj.prop = 'some value'; fn.prop = 'some value'; assert( obj.prop == fn.prop, 'Both are objects, both have the property.' ); assert(typeof(obj)
Solution 1:
That's because the direct type of a function is "function".
However, you missed this assertion:
fn instanceof Object // true
Btw, types such as "number" and "string" are strictly not descendants of Object, even though they are like objects in the sense that they have methods; just one of those things that makes JavaScript interesting :)
See also: typeof and its range of values.
Solution 2:
Functions are objects, but they are a particular type of object. typeof(fn) should evaluate to "function", which is a sub-type of "object".
Post a Comment for "Functions Are Object In Javascript?"