Search code examples
javascriptobjectconstructor

Why is it possible for functions to have properties like objects? Are functions also objects?


Object() is a constructor function, so I was wondering how come we can call functions on the Object constructor function, for example: Object.create().

In one context, Object seems to be a constructor function for creating objects, in another sense it seems to be an object itself that has methods and properties we can call.


Solution

  • What you call a constructor is more primitively put a function. Functions are objects as well in Javascript. As such, they can have properties. Properties can be functions:

    function Object() {}
    
    Object.create = function () {};
    

    All functions in Javascript exhibit this trait already:

    function foo() {}
    
    foo.call(bar);  # <- property .call of function object is a function