神刀安全网

Deep Dive into First Class Functions in JavaScript

Master First Class Functions In Javascript

June 13, 2016

Javascript is a multi-paradigm programming language that supports both inheritance through prototypes, and first class functions.

It is important to understand both of these paradigms to truly master the language. We’ve discussed inheritance in some depth before in the article above. In this post we will discuss how first class functions work in the language.

Because the use of first class functions has huge implications for how we write code in Javascript, this will probably be a multi part series.

In this post we will cover the basics before moving on to real world applications of the paradigm in future posts.

Let’s get started.

First Class functions – Lets define them:

In short, a language that supports first class functions is a language where a function has all the properties of or can inherit all of the other properties found within the language.

One fundamental application of first class objects In Javascript is unlike in many other languages, functions are full fledged objects.

So, in Javascript: function = everything an object is and can do + a bunch of other awesome stuff.

So, for example, with first class functions, you can:

  • Assign functions to variables.
  • Pass functions around like variables.
  • Create functions on the fly.

Everything you can do with other types like objects, strings, booleans, can all be done with functions.

Javascript Functions in Depth:

Ok, so let’s talk about how a function is composed, It’s unique characteristics and the characteristics it shares with Javascript Objects.

You should think of a function as an object, where your code is just a single attribute of that function, but certainly that is certainly not the only attribute your function has.

See the diagram below:

Deep Dive into First Class Functions in JavaScript

As you can see, we have our function object in the center. This function, being an object, is capable of having primitives such as strings, numbers and booleans, other functions, and other objects assigned to that function.

Here is an example, let’s start with the very basic function Greet()

To demonstrate the flexibility of functions in Javascript, I am going to assign a property to the function, in much the same way we would assign a property to a standard Javascript Object:

Woah! Cool right? Bet you have not seen that before.

Here I am actually using the function’s object properties, totally independent of it’s code properties.

We just added a property to a function. In other programming languages, this simply would not be possible.

As you can see, when I console.log the value ‘english’ prints to the screen.

A function has a name property.

Functions can be named. But they do not have to be. In Javascript, you can also have anonymous functions. These functions have no name, and are typically executed at run time, or assigned to another primitive, like a variable.

A function has code

So, in addition to all of the properties an object would typically have, a function has code you write that you can execute. This is the code found inside the function. As a day to day programmer, this is the feature of functions you are most familiar with.

You write a function, take arguments and modify that state of those arguments in some way before returning the code.

Making a function first class gives it a TON of features

Having a function that performs all of the basic actions a standard function would have, plus all of the features present in an Object or Class makes Javascript functions extremely powerful. Much of this blog will be dedicated to exploring these features!

For now, realize that Javascript functions are first class objects, and are capable of much, much more than most languages.

Stay tuned for more on this subject!

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Deep Dive into First Class Functions in JavaScript

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址