functions are object in javascript?

后端 未结 2 1133
故里飘歌
故里飘歌 2021-01-21 04:44
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.\" );
a         


        
相关标签:
2条回答
  • 2021-01-21 05:16

    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.

    0 讨论(0)
  • 2021-01-21 05:23

    Functions are objects, but they are a particular type of object. typeof(fn) should evaluate to "function", which is a sub-type of "object".

    0 讨论(0)
提交回复
热议问题