当前位置:网站首页>es6 arrow function explanation
es6 arrow function explanation
2022-07-29 12:20:00 【coldriversnow】
s6箭头函数的用法
箭头函数是es6的一种函数的简写方法.
如下:
var f = v = > v;
//等同于
var f = function(v){
return v;
}
var sum = (num1,num2) => num1+num2 ;
//等同于
var sum = function(num1,num2){
return num1+num2
}
[1,2,3].map(function (x) {
return x * x;
});
// 箭头函数写法
[1,2,3].map(x => x * x);//简洁了许多从例子我们可以看出,省略了function,花括号‘{}’用‘=>’代替了.这种写法更简洁了.
除了简洁之外,Arrow functions have another advantage,in the body of the functionthis的指向始终是指向定义它所在的对象,而不会指向调用它的对象,我们知道es5中的函数是谁执行它,它就指向谁.
es5 eg:
var countdown ={
'count':10,
'str':'hello!!!',
showstr(){
var _this = this;
var dom = document.getElementById('dom');
dom.innerHTML= _this.todouble(this.count);
setInterval(function(){
var dom=document.getElementById('dom');
dom.innerHTML=_this.todouble(_this.count);
_this.count --;
if(_this.count <0){
dom.innerHTML=_this.str;
}
},1000)
},
todouble(t){
var t = parseInt(t);
if(t<10){
return '0'+t;
}else{
return t;
}
}
}
countdown.showstr();As shown above, one is displayed after a countdownhellotext effect,在setInterval里面,如果我们直接写this的话,这个this是指向window的.因此我们需要在setIntervalsave before the function_this = this;
当我们使用es6the arrow function,就可以直接使用this了
es6 eg:
//es6的写法.
var countdown ={
'count':10,
'str':'hello!!!',
showstr(){
var dom = document.getElementById('dom');
dom.innerHTML= this.todouble(this.count);
setInterval(() => {
dom.innerHTML= this.todouble(this.count);;
this.count --;
if(this.count <0){
dom.innerHTML=this.str;
return false;
}
},1000)
},
todouble(t){
var t = parseInt(t);
if(t<10){
return '0'+t;
}else{
return t;
}
}
}
countdown.showstr();After changing the same code above to an arrow function, we are heresetInterval里面就可以直接使用this了.
箭头函数里面的this装换成es5后的代码如下:
// ES6
function fn() {
setTimeout(() => {
console.log('id:', this.id);
}, 100);
}
// ES5
function fn() {
var _this = this;
setTimeout(function () {
console.log('id:', _this.id);
}, 100);
}上面代码中,转换后的ES5Version notes,箭头函数里面根本没有自己的this,而是引用外层的this,由于箭头函数没有自己的this,So it can't be usedcall()、apply()、bind()这些方法去改变this的指向.
Note the use of arrow functions:
(1)函数体内的this对象,就是定义时所在的对象,而不是使用时所在的对象.
(2)不可以当作构造函数,也就是说,不可以使用new命令,否则会抛出一个错误.因为箭头函数的thisis determined by the object that defines it,An object's constructor is top-level,its outer layer,没有thisIt can be passed in for use by arrow functions.
(3)不可以使用arguments对象,该对象在函数体内不存在.如果要用,可以用Rest参数代替.
(4)不可以使用yield命令,因此箭头函数不能用作Generator函数.
边栏推荐
- WordPress 重置密码
- DAY 25 daily SQL clock 】 【 丨 different sex daily score a total difficulty moderate 】 【
- 测试环境要多少,从现实需求说起
- 【每日SQL打卡】DAY 20丨查询球队积分【难度中等】
- shell if else 使用
- DAY 20 daily SQL clock 】 【 丨 query difficulty moderate 】 【 team integral
- 路径依赖 - 偶然决策导致的依赖。
- 金仓数据库 KingbaseES 客户端编程接口指南 - ODBC 驱动使用
- Basic Concepts of Kubernetes
- 惠及6亿人 投资98亿 沿江高铁武宜段最新进展来了!
猜你喜欢
随机推荐
飞桨框架体验评测交流会,产品的使用体验由你来决定!
Path dependence - accidental decision to rely on.
RediSearch 发布 v2.4.10 & v2.4.11 版本
【一起学Rust | 基础篇】rust函数与流程控制详解
金仓数据库KingbaseES安全指南--6.8. SSPI身份验证
WordPress 编辑用户
QCon大会广州站它来了!独家定制双肩背包等你领取!
惠及6亿人 投资98亿 沿江高铁武宜段最新进展来了!
PL/SQL 面向对象
1.4、栈
How much is the test environment, starting from the actual needs
“祁东黄花菜”是国家地理标志保护产品吗? 蚂蚁新村7月29日答案
【多线程】——深入理解线程中断方式(interrupt)
获取list集合中重复的元素
【每日SQL打卡】DAY 23丨学生们参加各科测试的次数【难度简单】
GDB使用详解
593. 有效的正方形 : 简单几何运用题
金仓数据库KingbaseES客户端编程接口指南-ODBC(9. 疑难解答)
DAY 22 丨 page daily clock in SQL 】 【 recommend 【 difficulty moderate 】
socket+websocket






![[纯理论] FPN (Feature Pyramid Network)](/img/30/cfb6e3197bc2f4e7e0f1d492976c47.png)


