SoFunction
Updated on 2025-04-06

Three features of JS and functional languages

First of all, we need to have a concept: it does not support functions in a language, but 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 below:
1. Functions are operational elements
2. Save data in the function
3. Operations inside the function have no side effects on the function
1. Functions are operational elements
When calling an ordinary function, it can be understood abstractly as: a function is an operator, and the parameters passed in are operators;
However, when a function in JavaScript is used as a parameter of another function, it is referenced, and this "passed parameter" can be understood as an operation element. The conclusion is that (as "passed in parameter") functions have the meaning of an operational element, and "function parameters" are no different from ordinary parameters.

2. Save data in the function
In imperative languages, private variables (local variables) inside functions cannot be saved. From the perspective of program execution method, local variables are allocated on the stack, and after the function execution is completed, the occupied stack is released. Therefore, the data in the function cannot be saved.
In JavaScript functions, private variables within the function can be modified, and when "entered" again, the modified state will continue. The following example illustrates this feature:

Copy the codeThe 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();
  (get()); //100
  set(300);
  (get()); //300


An obvious benefit is that if a data can be continuously saved within a function, the function (as a constructor) can be used for calculation when assigned to an instance; and between multiple instances, since the data exists in different closures, there will be no impact on each other.
Explanation in object-oriented terms means that different instances have their own private data (copyed from a public data). The following example illustrates this feature:
Copy the codeThe code is as follows:

  function MyObject(){
      var value = 100;
      = function(){
          value = v;
      }
      = function(){
          (value);
      }
  }
  var obj1 = new MyObject();
  var obj2 = new MyObject();

  (300);
  (); //100;


3. Operations inside functions have no side effects on the function.
The meaning of this feature is:
* Functions use entry parameters to perform operations without modifying it (used as value parameters instead of variable parameters)
* The values ​​of other data outside the function will not be modified during the operation (such as global variables)
* After the operation is completed, the value is passed 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 reference and modification of global variables inside functions, and even declares global variables. This actually destroys its functional characteristics.
In addition, JavaScript also allows modifying objects and array members within functions—these members should be modified by object methods rather than other functions outside the object system.
Therefore: This feature of JavaScript can only be guaranteed through the developers' programming habits.