ES6 arrow function
语法:
() => { … } // 零个参数用 () 表示;
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的更多相关文章
- 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 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] 06. Arrow Function =>
ES6 arrow function is somehow like CoffeeScirpt. CoffeeScript: //function call coffee = -> coffee ...
- [ES6系列-02]Arrow Function:Whats this?(箭头函数及它的this及其它)
[原创] 码路工人 大家好,这里是码路工人有力量,我是码路工人,你们是力量. 如果没用过CSharp的lambda 表达式,也没有了解过ES6,那第一眼看到这样代码什么感觉? /* eg.0 * fu ...
- ES6 new syntax of Arrow Function
Arrow Function.md Arrow Functions The basic syntax of an arrow function is as follows var fn = data ...
- 廖雪峰js教程笔记5 Arrow Function(箭头函数)
为什么叫Arrow Function?因为它的定义用的就是一个箭头: x => x * x 上面的箭头函数相当于: function (x) { return x * x; } 箭头函数 阅读: ...
随机推荐
- Django - 一对多创建
1.新创建一个app python manage.py startapp app01 2.在django的setting.py中,填加新增的app名称 3.在app01的models.py中,添加代码 ...
- Robberies (01背包dp变形)
题意:一个强盗要抢劫银行又不想被抓到,所以要进行概率分析求他在不被抓的情况下能抢最多的钱.他给定T(样例个数),N(要抢的银行的个数),P(被抓的概率要小于P)Mj(强盗能抢第j个银行Mj元钱),Pj ...
- ThinkPHP5 自定义异常
1.配置config.php 自定义异常路径: // 默认AJAX 数据返回格式,可选json xml ...'default_ajax_return' => 'json', 'exceptio ...
- Delphi 10.3.2最新消息
官方已经发布消息,招内测人员了! https://www.barnsten.com/default/newsupdates/details?news_id=328 https://docs.googl ...
- 【Codeforces 522A】Reposts
[链接] 我是链接,点我呀:) [题意] 有人转载官方号的动态. 又有其他人转载其他人转载的动态. 问你最长的一条转载动态的链有多长. [题解] 用map把每个人的英文都转成小写的 然后从map中获取 ...
- Java8新特性之forEach遍历
参考文章: https://www.cnblogs.com/billyu/p/6118008.html
- 清北学堂模拟赛d6t5 侦探游戏
分析:简化一下题意就是给任意两对点连一条权值为0的边,求出每次连边后最小生成树的权值和*2/(n - 1) * n. 每次求最小生成树肯定会爆炸,其实每次加边只是会对最小生成树上的一条边有影响,也就是 ...
- Linux 下android环境的配置
Linux 下android环境的配置 1. JDK下载 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads- ...
- nyoj_18_The Triangle_201312071533
The Triangle 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure ...
- JLOI2018 记
2018JL省选记 又是一年省选.今年的我,依然好菜啊... [Day 0] 呼...好紧张,明天就省选了.下周就有学长退役了吧,机房又该恢复冷清了吧.只剩下为数不多的几个i7接送着来来往往的OIer ...