[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 ...
随机推荐
- .net core web api 获取request body的纯文本
本文代码 https://github.com/wuhaibo/readPlainTextDotNetCoreWepApi 总有些时候我们希望获得Request body 的纯文本 那么怎么做呢?很简 ...
- ubuntu安装过程记录
[DNS修改] 新下载的ubuntu 17.04 安装后DNS是指向谷歌DNS的,谷歌被屏蔽啦,所以无法解析域名.解决办法: ctrl+alt+t 启动终端 : sudo su 输入管理員密碼,或去 ...
- 安装 Apache 源代码包
把自己在网易博客的文章迁移过来 cd /lamp/httpd-2.2.9 ./configure --prefix=/usr/local/apache2/ --sysconfdir=/usr/loca ...
- HDU 5514.Frogs-欧拉函数 or 容斥原理
Frogs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- Vue 2.0 Application Sample
===搭建Demo=== http://blog.csdn.net/wangjiaohome/article/details/51728217 ===单页Application=== http://b ...
- 树形dp(poj 1947 Rebuilding Roads )
题意: 有n个点组成一棵树,问至少要删除多少条边才能获得一棵有p个结点的子树? 思路: 设dp[i][k]为以i为根,生成节点数为k的子树,所需剪掉的边数. dp[i][1] = total(i.so ...
- Problem H: 零起点学算法87——打印所有低于平均分的分数
#include<stdio.h> int main(){ ],b[]; while(scanf("%d",&n)!=EOF){ ; ;i<n;i++){ ...
- JavaScript 匹配字符串偶数位置的字符 及匹配 $ 符号
已知一个字符串#####,现需要替换偶数位置的#为&. function replaceDemo(){ var s = "1#2#3#4#5#"; var regex = ...
- oracle增加表空间的四种方法
1. 查看所有表空间大小 select tablespace_name,sum(bytes)/1024/1024 from dba_data_files group by tablespace_nam ...
- BaseLoadDataForNetFragment
/** * Fragment 加载网络请求 */ public abstract class BaseLoadDataForNetFragment extends Fragment { // 视图是否 ...