Functions and Functional Programming

Among all the key knowledge that must be mastered in JavaScript, functions are the most easily overlooked knowledge point when we are beginners. In the process of learning, many people and articles may tell you that object-oriented and prototype are important, but few people tell you that almost all the key and difficult points in object-oriented are closely related to functions.

Including the execution context, variable objects, closures, this, and more that I have introduced in my previous articles, they all revolve around the details of functions.

I know many people are eager to start learning object-oriented, modular, popular frameworks, and quickly become experts in their studies. But I can responsibly tell you that if you don’t understand these basic things about functions to a certain extent, your learning progress will definitely be difficult.

So, everyone must value functions!

Of course, the key and difficult points about functions have already been discussed in the previous articles. This article mainly summarizes the basic knowledge of functions and provides a preliminary understanding of functional programming thinking.

Function declarations, function expressions, anonymous functions, and self executing functions

The application of functions in practical development can generally be summarized as function declarations, function expressions, anonymous functions, and self executing functions.

Function declaration

In JavaScript, there are two ways of declaration: one is to declare variables using var/let/const, and the other is to declare functions using functions.

In the front-end basic advancement (3): variable object explanation, I have mentioned that in the process of creating variable objects, function declarations have a higher priority in execution order than variable declarations, which is commonly referred to as function declarations being executed earlier. Therefore, in the execution context, regardless of where the function is declared, it can be directly used in the same execution context.

Function expression

Unlike function declarations, function expressions are declared using var/let/const. Therefore, when confirming whether they can be used correctly, we must follow the rules of var/let/const to make judgments, that is, variable declarations. We know that using var for variable declaration is actually a two-step operation.

Anonymous function

Anonymous functions, as the name suggests, refer to functions that are not displayed for assignment operations. Its usage scenario is often passed as a parameter into another function.

Function parameter transfer method: pass by value

Do you remember the difference in replication between basic data types and reference data types? Basic data type replication refers to the direct replication of values, so after changes, they do not affect each other. However, when copying a reference data type, the reference saved in the variable object is copied, so the two copied references actually access values from the same heap memory. When one is changed, the other is naturally also changed.

It is precisely because of such differences that many people have a lot of confusion when understanding the way function parameters are passed. Is it passed by value or by reference? In fact, the conclusion is still passed by value, but when we expect to pass a reference type, what is actually passed is only the reference that the reference type is stored in the variable object.

Functional programming

Although JavaScript is not a purely functional programming language, it utilizes many features of functional programming. Therefore, understanding these features can help us better understand the code we write.

When we want to use a function, we usually want to encapsulate some functions, logic, etc. I believe everyone is familiar with the concept of encapsulation.

We usually accomplish something through function encapsulation.