语法:

() => { … } // 零个参数用 () 表示; 
x => { … } // 一个参数可以省略 (); 
(x, y) => { … } // 多参数不能省略 ();

当我们使用箭头函数时,函数体内的this对象,就是定义时所在的对象,而不是使用时(执行时)所在的对象。
并不是因为箭头函数内部有绑定this的机制,实际原因是箭头函数根本没有自己的this,它的this是继承外面的,因此内部的this就是外层代码块的this。

箭头函数有几个使用注意点。

(1)函数体内的this对象,就是定义时所在的对象,而不是使用时所在的对象。

(2)不可以当作构造函数,也就是说,不可以使用new命令,否则会抛出一个错误。

(3)不可以使用arguments对象,该对象在函数体内不存在。如果要用,可以用Rest参数代替。

rest语法也很简单,直接看例子:

function animals(a,...types){
console.log(types)
}
animals('cat', 'dog', 'fish') //["dog", "fish"]
 

ES6 arrow function的更多相关文章

  1. vue & lifecycle methods & this bug & ES6 Arrow function & this bind bug

    vue & lifecycle methods & this bug ES6 Arrow function & this bind bug bad fetchTableData ...

  2. ES6 Arrow Function & this bug

    ES6 Arrow Function & this bug let accHeadings = document.querySelectorAll(`.accordionItemHeading ...

  3. ES6 Arrow Function All In One

    ES6 Arrow Function All In One this const log = console.log; const arrow_func = (args) => log(`arg ...

  4. ES6 arrow function vs ES5 function

    ES6 arrow function vs ES5 function ES6 arrow function 与 ES5 function 区别 this refs xgqfrms 2012-2020 ...

  5. ES6 Arrow Function return Object

    ES6 Arrow Function return Object https://github.com/lydiahallie/javascript-questions/issues/220#issu ...

  6. [ES6] 06. Arrow Function =>

    ES6 arrow function is somehow like CoffeeScirpt. CoffeeScript: //function call coffee = -> coffee ...

  7. [ES6系列-02]Arrow Function:Whats this?(箭头函数及它的this及其它)

    [原创] 码路工人 大家好,这里是码路工人有力量,我是码路工人,你们是力量. 如果没用过CSharp的lambda 表达式,也没有了解过ES6,那第一眼看到这样代码什么感觉? /* eg.0 * fu ...

  8. ES6 new syntax of Arrow Function

    Arrow Function.md Arrow Functions The basic syntax of an arrow function is as follows var fn = data ...

  9. 廖雪峰js教程笔记5 Arrow Function(箭头函数)

    为什么叫Arrow Function?因为它的定义用的就是一个箭头: x => x * x 上面的箭头函数相当于: function (x) { return x * x; } 箭头函数 阅读: ...

随机推荐

  1. centos中安装tomcat

    1.先保证centos中安装了jre的环境. 2.上传tomcat的压缩包到root根目录. 3.切换到根目录 输入命令cd ~ , 然后 ll , 查看上传情况: 4.选中复制压缩文件,输入解压命令 ...

  2. 46.颜色+品牌下钻分析时按最深层metric进行排序

    主要知识点: 在做下钻分析时的排序     需求,以颜色进行bucket,这里bucket里面的doc以其各品牌的平均价格排序,     GET /tvs/sales/_search { " ...

  3. vue 中全局filter过滤器的配置及使用

    在项目中使用到的经常用到过滤器,比如时间,数据截取等过滤器,如果在每个.vue中都可以复制同一个过滤器,这可以达到目的,但是遇到方法有bug时就需要诸葛修改进入不同的页面修改,这样既费时又费力,优先可 ...

  4. BZOJ 4278 [ONTAK2015]Tasowanie (后缀数组)

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=4278 题解: 居然把后缀数组写成n^2的..我真厉害.. 想了无数种方法,最后发现就是 ...

  5. 手动部署tidb(二进制包)集群

    环境准备 系统:centos7.2 用户:root 主机配置ntp时间同步 主机配置ssh互信 3节点: hostname@ip 部署组件 tidb1@10.70.249.24 PD,TiKV,TiD ...

  6. ACDream - Xor pairs

    先上题目: Xor pairs Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Sub ...

  7. Spring整体架构

    Spring整体架构 Spring的整体架构 Spring框架是分层架构的,它包含了一系列的功能要素. Spring整体架构图 模块分类 1. Core Container Core Containe ...

  8. 洛谷 P2965 [USACO09NOV]农活比赛The Grand Farm-off

    P2965 [USACO09NOV]农活比赛The Grand Farm-off 题目描述 Farmer John owns 3*N (1 <= N <= 500,000) cows su ...

  9. ELECTRON开发环境配置方法

    1.下载并安装Node.js 下载地址:https://nodejs.org/en/download/current/ 安装之后进行验证 2.下载并这装electron 安装命令:npm instal ...

  10. spring boot项目Intellij 打包

    spring boot项目Intellij 打包 学习了:http://blog.csdn.net/hzt_fighting_up/article/details/78174291 在edit con ...