misterd89
misterd89

Reputation: 81

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.

Upvotes: 0

Views: 54

Answers (1)

deceze
deceze

Reputation: 522110

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

Upvotes: 2

Related Questions