Function object and Function in an object

Previously, we have talked about all things being an object in Javascript. But to what extent can we apply this to our beloved functions.

You can see this almost everywhere with the below syntax -

const getSum = (x, y) => {
  return x + y;

console.log(getSum(1, 2));
// 3

We have also touched upon how function is an object and how that helps in using closures and currying.

For now, let’s leave all those complex things behind. Function being an object implies that we can do this -

const getSum = new Function("x", "y", "{return x + y}");

console.log(getSum(1, 2));
// 3

We just define a function like we define an object, and pass variables and function body as parameters to the constructor.

You get the function object as output from a type of.

console.log(typeof getSum);
// function

However, you would also know that every object functions with a function in the form of a constructor.

const earth = { position: 3 };
console.log(typeof earth);
// object
console.log(typeof earth.constructor);
// function

console.log(typeof Date);
// function

Therefore, anything without a function is just so primitive (a silly joke and a fact(?)).

comments powered by Disqus