当前位置:网站首页>ES6 learning function (strict mode, higher-order function, closure)
ES6 learning function (strict mode, higher-order function, closure)
2022-07-19 08:38:00 【When can Xiaobai advance to success】
1、 The role of strict mode
- Eliminated JS Grammatical irrationality 、 Not rigorous , Reduced some weird behavior
- Eliminate some insecurity of code running , Ensure the safety of code operation .
- Improve compiler efficiency , Increase running speed .
- Disabled in ECMAScript Some of the syntax that may be defined in future versions of . For example, some reserved words :import,class...
Can be applied to The whole script or Individual functions in .
2、 Changes in strict patterns
1、this Point to the problem
//1. use var Statement
var num = 10;
console.log(num);// Report errors
//2. You cannot delete declared variables at any time
//delete num;
//3. In a function in the global scope this yes undefined( No window)
function fn(){
console.log(this);
}
//4. Constructors do not add new call ,this Will report a mistake
function Star(){
this.sex = 'nv';
}
//5. In the timer this Or point to window
setTimeout(function(){
console.log(this);
},1000);2、 Changes in functions
//1. There can be no formal parameters with duplicate names
function fun(a,a){
console.log(a+a);
}
fun(1,2);// Report errors
//2. You cannot declare a function within a code block that is not a function
if(ture)
{
function(){
}// Report errors
}3、 Higher order function
Higher order functions are functions that operate on other functions , It receives function As Parameters , Or output the function as a return value .
Function is also a data type , It can also be used as a parameter , Pass to another parameter to use .

// Higher order function - Function as parameter
function fn(a,b,callBack)
{
console.log(a+b);
callBack && callBack(a,b);
}
fn(1,2,function(a,b){
console.log('this is callback');
});4、 Closure
1、 Closure definition
Definition : Closure is A function that has access to a variable in the scope of another function function .( Closure is a phenomenon )
F12 Check whether there are closures

closure Closure .
2、 The function of closures

stay fn Outside scope visit fn Internal local variables .
Or use higher-order functions .
// Closure :fn External scopes can access fn Internal local variables .
function fn(){
var num = 10;
return function(){
console.log(num);
};
}
var f = fn();
f();The function of closures : It extends the scope of variables .
5、 Shallow copy and deep copy
Shallow copy : Copy only one layer , Deeper object level copy only references .
Deep copy : Copy multiple layers , Every level of data is copied .
边栏推荐
猜你喜欢

Finishing of key concepts on deep neural networks exercises in the fourth week of in-depth learning

ES6学习-函数(严格模式,高阶函数,闭包)

WPF 3D application building (Foundation)

Baidu Apollo

oop_引用类型变量传值

Redis 概述安装

Enjoy JVM -- knowledge about GC garbage collection

matlab导入小数点后9位以上的浮点数

How to position the circle of friends? Three core steps to build a circle of friends selling popular products

6-9 vulnerability exploitation telnet login rights lifting
随机推荐
Eureka基础知识
matlab导入小数点后9位以上的浮点数
instanceof的另类写法示例说明
力扣1669合并两个链表笔记
6-9 vulnerability exploitation telnet login rights lifting
Viewing the technology stack of distributed system from the crash report of station B
JS学习笔记04-05——构造函数的修改以及使用工厂方法创建
60、wsgiref手写web框架+jinja2模块初识
Wvppro-zlm-gb21818-camera
be vigilant! Another phishing attack: uniswap stolen $8.1 million
Solution to sudo PIP install gevent installation failure
ES6学习-函数(严格模式,高阶函数,闭包)
Baidu Apollo
Visual studio 2022 (vs 2022) cannot read memory
1. Decision tree
6-9漏洞利用-Telnet登陆提权
SPARK闲杂--为什么复用Exchange和subquery
3D激光SLAM:ALOAM---帧间里程计代码解读
RestTemplate
Convex mirror 3D glass contour scanning