Home > Web Front-end > JS Tutorial > body text

Three Characteristics of JS and Functional Languages_Basic Knowledge

WBOY
Release: 2016-05-16 16:56:55
Original
1070 people have browsed it

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:

Copy code 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


An obvious benefit is that if a data can be persisted within a function, then the function (as a constructor) can be used when assigned to an instance These data perform operations; and between multiple instances, since the data exists in different closures, they will not affect each other.
Explained in object-oriented terms, it means that different instances have their own private data (copied from a certain public data). The following example illustrates this feature:
Copy code 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 within the function have no side effects outside the function
This The meaning of this feature is:
* The function uses the entry parameter to perform operations without modifying it (used as a value parameter instead of a variable parameter)
* The value of other data outside the function will not be modified during the operation ( For example, global variables)
* After the operation is completed, the value is transferred to the external system through "function return"

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.

Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template