[ES6] 06. Arrow Function =>
ES6 arrow function is somehow like CoffeeScirpt.
CoffeeScript:
//function call
coffee = -> coffee()
coffee=(message) -> coffee("Yo"), coffee "Yo"
coffee=(message, other) -> coffee("Yo", 2), coffee "Yo", 2
Now we rewrite a ES5 function to ES6 function:
ES5:
var greeting = function(message, name){
return message + name;
}
ES6:
First thing in ES6, we can remove the function keyword and add => on the right side of the params:
var greeting = (message, name) => {
return message + name ;
}
Second, we can remove 'return' and {};
var greeting = (message, name) => message + name
Example 1:
var f = () => 5;
// 等同于
var f = function (){ return 5 };
Example 2:
//ES6
var msg = message => "Hello Es6"//ES5
var msg = function(message){
return "Hello Es6";
}
Example 3:
// 正常函数写法
[1,2,3].map(function (x) {
return x * x;
}); // 箭头函数写法
[1,2,3].map(x => x * x);
Example 4:
// 正常函数写法
var result = values.sort(function(a, b) {
return a - b;
}); // 箭头函数写法
var result = value.sort((a,b)=> a- b)
=> function helps to sovle the context problem:
//ES5
var deliveryBoy = {
name: "John",
receive: function(){
var that = this;
this.handleMessage("Hello", function(message){
//Here we have a very amazing handle function
//which combine the name and message
console.log(message + ' '+that.name);
});
},
handleMessage: function(message, handler){
handler(message);
}
}
deliveryBoy.receive();
In the code, we see:
console.log(message + ' '+that.name);
We use var that = this; and that.name to refer to "John". It looks quite confusing.
Arrow function helps us out of this:
var deliveryBoy = {
name: "John",
receive: function(){this.handleMessage("Hello", message => console.log(message + ' '+this.name));
},
handleMessage: function(message, handler){
handler(message);
}
}
deliveryBoy.receive();
Inside the code, we still use this.name to refer "John". This is because, => refer to the deliveryBoy object.
箭头函数有几个使用注意点。
- 函数体内的this对象,绑定定义时所在的对象,而不是使用时所在的对象。
- 不可以当作构造函数,也就是说,不可以使用new命令,否则会抛出一个错误。
- 不可以使用arguments对象,该对象在函数体内不存在。
[ES6] 06. Arrow Function =>的更多相关文章
- 理解es6 中 arrow function的this
箭头函数相当于定义时候,普通函数.bind(this)箭头函数根本没有自己的this,导致内部的this就是定义时候的外层代码块中的this.外层代码块中的this,则取决于执行时候环境上下文cont ...
- 廖雪峰js教程笔记5 Arrow Function(箭头函数)
为什么叫Arrow Function?因为它的定义用的就是一个箭头: x => x * x 上面的箭头函数相当于: function (x) { return x * x; } 箭头函数 阅读: ...
- vue & lifecycle methods & this bug & ES6 Arrow function & this bind bug
vue & lifecycle methods & this bug ES6 Arrow function & this bind bug bad fetchTableData ...
- ES6 Arrow Function & this bug
ES6 Arrow Function & this bug let accHeadings = document.querySelectorAll(`.accordionItemHeading ...
- [ES6系列-02]Arrow Function:Whats this?(箭头函数及它的this及其它)
[原创] 码路工人 大家好,这里是码路工人有力量,我是码路工人,你们是力量. 如果没用过CSharp的lambda 表达式,也没有了解过ES6,那第一眼看到这样代码什么感觉? /* eg.0 * fu ...
- ES6 Arrow Function All In One
ES6 Arrow Function All In One this const log = console.log; const arrow_func = (args) => log(`arg ...
- ES6 arrow function vs ES5 function
ES6 arrow function vs ES5 function ES6 arrow function 与 ES5 function 区别 this refs xgqfrms 2012-2020 ...
- ES6 Arrow Function return Object
ES6 Arrow Function return Object https://github.com/lydiahallie/javascript-questions/issues/220#issu ...
- ES6 new syntax of Arrow Function
Arrow Function.md Arrow Functions The basic syntax of an arrow function is as follows var fn = data ...
随机推荐
- javascript高程笔记:逻辑与和逻辑或
逻辑与和或 逻辑与 当 && 前后两个操作数都是布尔值,无可厚非,同时为true才为true.与其他强类型语言不同的是,javascript逻辑与前后的操作数可以应用于任何类型. 而且 ...
- windows10 自带的OpenSSH Client(Beta)
我不知道其他版本有没有 ,我是windows10 专业版,版本1709,OS内部版本16288.1 安装过程: 1.我的电脑上面的卸载或更改程序 2.管理可选功能 3.添加功能 4.重启电脑,搞定 O ...
- nginx 开启 gzip
gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_comp_level 2; gzip_types text/plain applicatio ...
- 关于自建yum源拾遗
yum参数解释[updates]--->源name=CentOS-$releasever - Updates --->源的名字baseurl=http://mirror.centos.or ...
- For input string: "..."
之前在项目中通过EL表达式从cart 中取出itemPirce这个值时始终报:For input string: "${cart.itemPrice / 100}" 错误. 事故原 ...
- 在Pygtk和Glade使用Gtkbuilder
最近开始学习python的GUI,选择了Pygtk,试着用Glade设计界面,项目文件采用Gtkbuilder格式,网上的教程大部分是使用Libglade,所以用xml方式读取.glade文件: wT ...
- Hive 空指针(NPE)异常
空指针NullPointerException 1 Hive之前的一些BUG [HIVE-9430] - NullPointerException on ALTER TABLE ADD PARTITI ...
- Problem A: 指针:调用自定义交换函数,完成三个数整从小到大排列
#include<stdio.h> int swap(int *p1,int *p2) { int flag; if(*p1>*p2) { flag=*p1; *p1=*p2; *p ...
- [转]Spring MVC 事务配置
Spring MVC事务配置 要了解事务配置的所有方法,请看一下<Spring事务配置的5种方法> 本文介绍两种配置方法: <tx:advice/>就是告诉事务管理器:怎么做 ...
- storm一键脚本
1一键脚本 #!/bin/bash # 1 声明 #-------------------------------------- #请在strom/bin/下执行脚本 #supervisor-host ...