In the scripting language JavaScript, the definition of a function is a block of code that is driven by an event or reusable when it is called. In JavaScript's standard ECMAscript, functions are expressed as statements that can be run anytime, anywhere. I personally do not agree with ECMA's statement, because the function will only be executed when a call occurs, otherwise it will be a lifeless piece of code.
Let’s understand the functions in detail.
(1) First of all, the definition of the function: the definition of the ECMAscript function is composed of four parts: the keyword function name (parameter) {body; return (return value)};, but the definition of the function in the script language is divided into three ways:
Form 1: function function name (parameter) {body; return value}; This definition method is the same as that of ECMAscript;
Form 2: var function name = function (parameter) {body; return value};
Form 3: var function name = new Function (parameter) {body; return value};
There are only these three ways to define functions in Javascript. Let’s talk about the precautions and specific usage of each definition method: the first way of definition is the simplest and easiest to understand. It simply defines a function for us for us to use in the next programming; the second way is to implement it by constructing an anonymous function and passing this anonymous function to a function variable we define. It is very common in closures, so that in the global domain, we can use inner-level functions to form a closure structure. The third way is to create a new memory space to store our anonymous function and assign a value to the function name we defined. This function is created based on our anonymous function. This method is mainly used in prototypes.
In fact, through observation, we will find that these three definition methods seem to be closely linked. It feels like the third first method is a combination of the first two and the third one. The actual thing is also what you see, so we just need to know these three definition methods and just know when they will use them. Furthermore, what I want to say is that the data types of these three definition methods are based on basic types, and are the same as data based on object types. In object-based data types, we call functions objects. When you come into contact with the definition of objects, it is obvious that they are defined in basically the same way. But the function name is called an object, and the definition form is very similar. Remember, an object is a function, and a function is an object.
(II) Several special functions
(1) Anonymous functions
The so-called anonymous function is a function without a function name. The point of this kind of function is that its call is difficult, which ensures security. So how should we call anonymous functions? Let’s take a look at an example:
By passing anonymous functions as arguments to the add variable, we can call our anonymous functions.
(2) Self-modulating function
Self-keying functions are also a type of anonymous functions. They are not passed as parameters to other variables, nor do they have their own function names. In other words, they cannot be called by others, they can only call themselves.
The way to write in the figure is to use the self-keying function. This function is surrounded by two brackets. The function of the first bracket is to encapsulate it, encapsulating the anonymous function we wrote. The second bracket is called, and we can also initialize it, just like the writing method of the second self-keying function.
In fact, self-modulation functions are mainly used to implement one-time functions, which means that they only run once during the execution cycle, so when we initialize the web page, we can consider using self-modulation functions.
(3) Callback function
A callback function is a function that passes itself as a return value or actual parameter.
The functions a() and b() are the applications of callback functions.
(4) Internal functions
This is a sentence, which is a type of function that exists inside a function.
(III) Advantages of functions
1. A large number of repeated statements are written in the function and can be called repeatedly;
2. Simplify the programming language and make programming modular;
3. Optimize the code structure;
(IV) Things to note
1. When writing a function, you can call it first and write it; because JavaScript will implicitly declare all our functions and variables at the beginning of the program;
2. The function will not be executed by itself. Only when a function call occurs will the space be allocated and used;
3. When the function names are the same, the function written below is executed by default;
4. If the function name is composed of one word, the first letter is lowercase. If there are multiple words, the first letter of other words after the first word is capitalized;
(V) Variable domain of function
1. Global variables
Variables written in the global domain are called global variables;
2. Local variables
Variables defined inside a function and declared with var are called local variables; if they are defined inside a function but not declared with var, they are considered as global variables.
We must know clearly that learning JavaScript is learning various functions, so we must have a deeper understanding of functions. Otherwise, in the future learning process, we will encounter nesting of various functions, and various functions are mixed together, which will make our own messy bag. This simple article is my understanding of functions. I hope these simple views will be helpful to you in understanding functions.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.