First of all, there is a concept: it is not a language that supports functions. This language can be called a "functional language". In addition to being called, functions in functional languages also have some other properties. There are three points:
1. Functions are operands
2. Save data within the function
3. Operations within the function have no side effects outside the function
1. Functions are operands
When calling a normal function, it can be understood abstractly as: the function is an operator, and the parameters passed in are the operands;
But when a function in JavaScript is used as a parameter of another function, a reference is passed, and this "incoming parameter" can be understood as an operand. The conclusion is that functions (as "incoming parameters") have the meaning of operands, and "function parameters" are no different from ordinary parameters.
2. Save data within the function
In imperative languages, private variables (local variables) within functions cannot be saved. From the perspective of program execution, local variables are allocated on the stack, and after the function execution ends, the occupied stack is released. Therefore the data within the function cannot be saved.
In a JavaScript function, private variables within the function can be modified, and when "entering" the function again, the modified state will continue. The following example illustrates this feature:
Copy the code code 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
An obvious benefit is that if a piece of data can be persisted within a function, then the function (as a constructor) can use the data to perform operations when assigned to an instance; and between multiple instances, since the data exists in different In closure, they will not affect each other.
To explain it in object-oriented terms, it means that different instances have their own private data (copied from some public data). The following example illustrates this feature:
Copy the code code 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 within the function have no side effects outside the function
The meaning of this feature is:
* The function uses the entry parameter to perform operations without modifying it (used as a value parameter rather than a variable parameter)
* The value of other data outside the function (such as global variables) will not be modified during the operation.
* After the operation is completed, the value is transferred to the external system through "function return"
Such functions have no side effects on the external system during operation. However, we noticed that JavaScript allows global variables to be referenced and modified inside functions, and global variables can even be declared. This actually destroys its functional characteristics.
In addition, JavaScript also allows object and array members to be modified within functions - these members should be modified by object methods rather than other functions outside the object system.
So: This feature of JavaScript can only be guaranteed by the programming habits of developers.