The content of this article is the reading notes I made when I read "The essence of JavaScript language and programming practices". The reading notes I wrote are too profound. First of all, I must have a concept: it is not a language that supports functions. This language can be called a functional language ". Functions in Functional Languages have some other properties in addition to being called. There are three points:
1. The function is an operational element.
2. Save data in the function
3. the operation in the function has no side effects on the function.
I. functions are operation elements
When a common function is called, it can be abstracted as: a function is an operator, and the input parameter is an operational element;
However, when a function in JavaScript is used as a parameter of another function, it passes references. This "Incoming parameter" can be understood as an operational element. The conclusion is that a function (as an input parameter) has the meaning of an operation element, and "function parameters" are no different from common parameters.
2. Save data in the function
In imperative languages, private variables (local variables) in functions cannot be saved. In terms of Program Execution Mode, local variables are allocated on the stack. After function execution, the occupied stack is released. Therefore, the data in the function cannot be saved.
In a JavaScript function, private variables in the function can be modified, and the modified state will continue when the function is "entered" again. The following example illustrates this feature:
The Code is as follows:
Var set, get;
Function MyFunc (){
Var value = 100;
Function set_value (v ){
Value = v;
}
Function get_value (){
Return value;
}
Set = set_value;
Get = get_value;
}
MyFunc ();
Console. log (get (); // 100
Set (300 );
Console. log (get (); // 300
One obvious benefit is that if a data can be continuously stored in the function, the function (as the constructor) can be used for computation when assigned to the instance; data exists in different closures between multiple instances, so they do not affect each other.
In object-oriented terms, different instances have their own private data (copied from a certain public data ). The following example illustrates this feature:
The Code is as follows:
Function MyObject (){
Var value = 100;
This. setValue = function (){
Value = v;
}
This. showValue = function (){
Console. log (value );
}
}
Var obj1 = new MyObject ();
Var obj2 = new MyObject ();
Obj2.setValue (300 );
Obj1.showValue (); // 100;
3. Operations in the function have no side effects on the function.
This feature has the following meanings:
* The function uses the entry parameter for calculation without modifying it (used as a value parameter rather than a variable parameter)
* Other data values (such as global variables) outside the function are not modified during the operation)
* After the operation is completed, pass the value to the external system through "function return ".
Such a function has no side effects on the external system during operation. However, we have noticed that JavaScript allows reference and modification of global variables within the function, and even can declare global variables. This actually destroys its functional features.
In addition, JavaScript allows you to modify objects and group members in a function-these Members should be modified by object methods rather than other functions outside the object system.
Therefore, the JavaScript feature can only be ensured through developers' programming habits.