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 the following three points:
1. Functions are operands
2. Save data within functions
3. Operations within functions have no side effects outside the function
1. Functions are operands
When a normal function is called, it can be understood abstractly as: the function is an operator, and the parameters passed in are operands;
But when a function in JavaScript is used as a parameter of another function, It is passed by reference, 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. Saving 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 JavaScript functions, 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:
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
obj2.setValue(300);
obj1.showValue(); //100;
Such a function has 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.