First Class Function In JavaScript

Some days ago, I was reading about Node.js, followed by hearing about the “First Class Function in JavaScript”, which was a new term for me. I searched about this on the internet and I found it more interesting, than I expected.Thus, I thought to write an article on this topic.

What is meant by “First Class Function” in JavaScript?

According to Wikipedia “In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens. Specifically, this means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures”. Functions in JavaScript are the first class objects, which means JavaScript functions are just a special type of the object, which can do all the things that regular objects do like string, object.

We can do the following with the functions in JavaScript.

  • Pass a parameter.
  • Return the function from another function.
  • Assign a function to a variable.
  • Assign function into an array.
  • Include in any data structure.
  • Function are an instance of an object.
  • Function can have the properties.

Now, w will take some example for better a explanation of all the points which we defined above.

Function Instance of Object

We know that JavaScript is an Object Oriented Programming language. Each and everything in JavaScript is treated as an object. Thus, a function is also an instance of an object. Let’s take an example:

Example

code

In this example, we can see whether if the condition is satisfied or not, since I asked if that function is having an instance of the objects or not.

Function can have properties

This was very interesting for me, that the function can have properties and function. It can link back to its constructor function.

Example

code

In the example, given above, we assign Name, Age and City properties to CallFun . How can it be possible that a function can have the properties? The reason behind it is that the function works as an object in JavaScript, so you assign the properties to the function.

Pass function as Parameter

You can pass a function as parameter to another function.

Example

code

In the example, mentioned above, we created Call1, Call2 and CallMe function, followed by passing Call1 and Call2 as the parameters of CallMe function.

Assign function to Variable:

We can assign any function to a variable.

Example

code

Return Function from another function

We can return a function from another function just like any other object integer or string.

Example

code

Assign Function to Array

You can assign a function to any array just like an integer or a string.

Example

code

In this example, we assign four functions to an array and call each function, using the index of an array.

Assign function as a value of the key in an object

This is one of the most known uses of a function in JavaScript. We can assign the function as a value of a key in any object.

Example

code

Partial Application

I think this is the most useful property of a function, where we can use a function in the partial Applications. In the previous example, we understood, that we can return a function from another function. Let's take another example.

Example

code

Partial Application is used to return a function, which is not complete and require some user input to compute the result. In this example, we create Add function, which takes an input and returns a function. As we passed 10 as a parameter, this value is stored in the function, which is returned from Add function and next time, when we pass 20 and 10, which are already stored and is added with 20; 30 is returned as an output.

Prototype Inheritance

Every JavaScript object has a prototype. The prototype is also an object. JavaScript is an object oriented programming language. It also supports the inheritance, but the type of inheritance is a prototype inheritance.

What is Prototype Inheritance

Prototype inheritance means there are no classes; an object inherits another object. JavaScript objects inherit the properties and methods from their prototype.

code

In this example, we create two function constructors“Animal” and “Cow”. In the prototype of “Cow”, we assign the “Animal” object, which means __proto__ link of Cow object link to the Animal object. When we try to access the “leg” and “Category” property, using the object of Cow, the interpreter doesn’t find it in Cow, it searches in __proto__ link, where it found the Category and leg property.

Output

Output

We can see that __proto__ property of Cow link to the Animal object. This is an example of prototype inheritance.

code

This is an example of prototype chain, where a prototype of an object inherits another object and this object is also assigned to the prototype property of another object. When an interpreter searches for Country_Name, first of all, it searches in object of City, when it doesn’t find it, it goes to __Proto__ property and finds  a link to State object, it searches for Country_Name in the state object and when again, it doesn’t find the Country_Name, it again goes to __proto__ property of the State object and finds a link to the Country Object, where it finds the Country_Name. Interpreter searches in __proto__ property, until it doesn’t find __proto__ as NULL.

code

In this example, we create State_ and Country_ property for __proto__ and assign the prototype of the Country object to Country_ and the prototype of the State object to State_. When you check the prototype of the City object, you find that it contains the definition of both Country and State objects.

Thanks for reading the article.


Similar Articles