This page looks best with JavaScript enabled

Function object and Function in an object

 ·   ·  ☕ 1 min read

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 -

1
2
3
4
5
6
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 -

1
2
3
4
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.

1
2
console.log(typeof getSum);
// function

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

1
2
3
4
5
6
7
8
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(?)).

Stay in touch!
Share on

Prashanth Krishnamurthy
WRITTEN BY
Prashanth Krishnamurthy
Technologist | Creator of Things