Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we prove that javascript functions are objects?

i am using

var foo = function(){}

typeof(foo) - returns function instead of Object

while javascript concept tell that all function in javascript are objects?

like image 685
Atul Kumar Avatar asked Sep 20 '26 00:09

Atul Kumar


1 Answers

First and foremost, there's the fact that the specification explicitly states they're objects, in a lot of places, including here where it defines the term "function":

function

member of the Object type that may be invoked as a subroutine

Empirically, there are lots of ways to prove it, but one of the simplest is to assign a value to a property on it:

var foo = function() { };
foo.myProperty = "my value";
console.log(foo.myProperty); // "my value"

Or use one as a prototype (which is unusual, but possible), which proves it as only objects can be prototypes:

var foo = function() { };
var obj = Object.create(foo);
console.log(Object.getPrototypeOf(obj) === foo); // true
like image 118
T.J. Crowder Avatar answered Sep 21 '26 14:09

T.J. Crowder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!