当前位置:网站首页>JS pure function
JS pure function
2022-07-06 08:29:00 【Wind billows】
Pure function
1. This function when the same input value , Always produce the same output . The output of the function is independent of the context state of the current running environment .
Pure functions are like functions in Mathematics , Function maps input parameters to return values , in other words , For each set of inputs , There is an output .
function test(a){
console.log(a*a);
}
test(10);
test(10);
test(10);
test(10);
2. The running process of this function does not affect the running environment , That is, no side effects ( Such as trigger event 、 launch http request 、 Print /log etc. ).
A function execution process produces externally observable changes to , Then say , This function has side effects . and Pure functions do not produce any observable side effects , That is, it cannot change any external state .
var num = 18;
function compare(x){
return x > num;
}
console.log(compare(20))
// Not a pure function
// Revise it
var num = 18;
function compare(x){
return x > 18;
}
console.log(compare(20))
// Pass two parameters
var num = 18;
function compare(x,num){
return x > num;
}
console.log(compare(20,num))
// Not using external num But within its scope num
3. If it's a reference type ?
var arr = [];
function add(_arr){
var obj = {
name:'cts'};
_arr.push(obj);
return _arr;
}
add(arr);
console.log(arr);
// Not a pure function Reference type
// Revise it
var arr = [{
name:'bailibn'}];
function add(_arr){
var obj = {
name:'cts'};
// Create a new arr
var newArr = [];
// take arr Clone the contents of
for(var i = 0;i<_arr.length;i++){
// Clone by cloning
newArr[i] = _arr[i];
}
newArr.push(obj);
return newArr;
}
console.log(add(arr))
// To simplify the
function add(x,y){
return x+y;
}
var num1 = 1;
var num2 = 2;
add(num1,num2);// Pure function
var GArr = [];
function add(arr){
arr.push(1);
return arr;
}
add(gArr);// Not a pure function
// A lot of bullshit , What's the use of pure functions ?
Simply speaking , That is, when the output of a function is not affected by the external environment , At the same time, it does not affect the external environment , This function is a pure function , That is, it only focuses on logical operations and mathematical operations , The same input always gets the same output .
javascript Built in functions have many pure functions , There are also many non pure functions .
Pure function :
Array.prototype.slice
Array.prototype.map
String.prototype.toUpperCase
Meromorphic function :
Math.random
Date.now
Array.ptototype.splice
Pure function action
JavaScript It is easy to create global variables in , These variables can be accessed in all functions . This is also a cause bug Common causes of , Because any part of the program may modify global variables, resulting in abnormal bold style of function behavior Pure functions are very easy to unit test , Because you don't need to think about context , Just consider the inputs and outputs边栏推荐
- China's high purity aluminum target market status and investment forecast report (2022 Edition)
- 2022 Inner Mongolia latest construction tower crane (construction special operation) simulation examination question bank and answers
- vulnhub hackme: 1
- C语言自定义类型:结构体
- 华为云OBS文件上传下载工具类
- Research Report on Market Research and investment strategy of microcrystalline graphite materials in China (2022 Edition)
- China high purity silver nitrate Market Research and investment strategy report (2022 Edition)
- Restore backup data on S3 compatible storage with br
- ROS编译 调用第三方动态库(xxx.so)
- 2022.02.13 - NC002. sort
猜你喜欢
[research materials] 2021 China online high growth white paper - Download attached
C language - bit segment
Use Alibaba icon in uniapp
2022.02.13 - 238. Maximum number of "balloons"
Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
The resources of underground pipe holes are tight, and the air blowing micro cable is not fragrant?
How to use information mechanism to realize process mutual exclusion, process synchronization and precursor relationship
JVM performance tuning and practical basic theory - Part 1
2022 Inner Mongolia latest construction tower crane (construction special operation) simulation examination question bank and answers
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
随机推荐
leetcode刷题 (5.28) 哈希表
matplotlib. Widgets are easy to use
Report on Market Research and investment prospects of China's silver powder industry (2022 Edition)
JVM performance tuning and practical basic theory - Part 1
Fibonacci sequence
Restore backup data on S3 compatible storage with br
Summary of MySQL index failure scenarios
指针进阶---指针数组,数组指针
Migrate data from a tidb cluster to another tidb cluster
From monomer structure to microservice architecture, introduction to microservices
IOT -- interpreting the four tier architecture of the Internet of things
LDAP应用篇(4)Jenkins接入
LDAP Application Section (4) Jenkins Access
使用 TiUP 升级 TiDB
IoT -- 解读物联网四层架构
Grayscale upgrade tidb operator
Pointer advanced --- pointer array, array pointer
[research materials] 2021 live broadcast annual data report of e-commerce - Download attached
China dihydrolaurenol market forecast and investment strategy report (2022 Edition)
Restore backup data on S3 compatible storage with tidb lightning