arrow function
简介
JavaScript 中,函数可以用箭头语法(”=>”)定义,有时候也叫“lambda表达式”。这种语法主要意图是定义轻量级的内联回调函数。例如:
// Arrow function:
[5, 8, 9].map(item => item + 1); // -> [6, 9, 10]
// Classic function equivalent:
[5, 8, 9].map(function(item) {
return item + 1;
}); // -> [6, 9, 10]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
当箭头函数有一个参数时,参数两边的括号是可有可无的,但是还是有括号看起来看清楚。
const foo = bar => bar + 1;
const bar = (baz) => baz + 1;
- 1
- 2
箭头函数不带参数时,必须要用括号,比如:
const foo = () => "foo";
- 1
如果函数体不是只一行,应该用花括号,并显式地返回(如果需要返回值)。
const foo = bar => {
const baz = 5;
return bar + baz;
};
foo(1); // -> 6
- 1
- 2
- 3
- 4
- 5
arguments object
箭头函数不会暴露 argument 对象,所以,argument 将简单地指向当前scope内的一个变量。
arguments object 是所有函数中的一个本地变量。你可以通过 arguments 对象引用函数的入参。这个对象包含传给这个函数的每个入参的入口,索引从0开始,例如:
arguments[0]
arguments[1]
arguments[2]
const arguments = [true];
const foo = x => console.log(arguments[0]);
foo(false); // -> true
- 1
- 2
- 3
- 4
基于此,箭头函数也不知道它的调用者。
当缺少arguments object时,可能会有所限制(极少数情况),其余的参数一般可以做为替代。
const arguments = [true];
const foo = (...arguments) => console.log(arguments[0]);
foo(false); // -> false
- 1
- 2
- 3
- 4
绑定this的值
箭头函数是 lexically scoped,这意味着其 this 绑定到了附近scope的上下文。也就是说,不管this指向什么,都可以用一个箭头函数保存。
看下面的例子, Cow 类有一个方法在1秒后输出sound。
class Cow {
constructor() {
this.sound = "moo";
}
makeSoundLater() {
setTimeout(() => {
console.log(this.sound);
}, 1000);
}
}
var myCow = new Cow();
var yourCow = new Cow();
yourCow.sound = "moooooo";
myCow.makeSoundLater();
yourCow.makeSoundLater();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
在 makeSoundLater() 方法中,this 指向当前 Cow 对象的实例。所以在这个例子中当我们调用 myCow.makeSoundLater(), this 指向 myCow。然后,通过使用箭头函数,我们保存了 this,这样我们就可以在需要时引用 this.sound 了。将会输出 “moo”,而不是yourCow.makeSoundLater()输出的“moooooo”。
隐式返回值
箭头函数可以通过省略掉小括号做到隐式返回值。
const foo = x => x + 1;
foo(1); // -> 2
- 1
- 2
当使用隐式返回时,Object Literal 必须用花括号括起来。
Object Literal 是用花括号括起来的,分号隔开的 k-v 对象列表。
const foo = () => { bar: 1 } // foo() returns undefined
const foo = () => ({ bar: 1 }) // foo() returns {bar: 1}
- 1
- 2
显示返回值
const foo = x => {
return x + 1;
}
foo(1); // -> 2
- 1
- 2
- 3
- 4
- 5
语法
x => y // Implicit return
x => { return y } // Explicit return
(x, y, z) => { ... } // Multiple arguments
(() => { ... })() // Immediately-invoked function expression
arrow function的更多相关文章
- 廖雪峰js教程笔记5 Arrow Function(箭头函数)
为什么叫Arrow Function?因为它的定义用的就是一个箭头: x => x * x 上面的箭头函数相当于: function (x) { return x * x; } 箭头函数 阅读: ...
- JavaScript学习笔记(十二)——箭头函数(Arrow Function)
在学习廖雪峰前辈的JavaScript教程中,遇到了一些需要注意的点,因此作为学习笔记列出来,提醒自己注意! 如果大家有需要,欢迎访问前辈的博客https://www.liaoxuefeng.com/ ...
- ES6 new syntax of Arrow Function
Arrow Function.md Arrow Functions The basic syntax of an arrow function is as follows var fn = data ...
- arrow function and bind
Can you bind arrow functions? https://stackoverflow.com/questions/33308121/can-you-bind-arrow-functi ...
- JS面试Q&A(续2): Rest parameter,Arrow function 等
rest parameter 和 Destructuring assignment. function fun1(...theArgs) { console.log(theArgs.length);} ...
- arrow function、function.apply
An arrow function expression has a shorter syntax than a function expression and does not have its o ...
- Arrow function restore
Arrow function restore 为什么叫Arrow Function?因为它的定义用的就是一个箭头: x => x * x 上面的箭头函数相当于: function (x) { r ...
- [ES6] 06. Arrow Function =>
ES6 arrow function is somehow like CoffeeScirpt. CoffeeScript: //function call coffee = -> coffee ...
- 10th week task -3 Arrow function restore
Arrow function restore 为什么叫Arrow Function?因为它的定义用的就是一个箭头: x => x * x 上面的箭头函数相当于: function (x) { r ...
随机推荐
- 随手记一 2018/04/23 Ajax基础了解
1.什么是ajax? 主要目的是用来实现客户端和服务器之间的异步通信,实现页面的局部刷新 2.同步和异步! 同步:当多个线程同时向一个数据发送请求时,必须是A先执行完毕才可以给B,会出现阻塞的情况,但 ...
- Extundelete 数据恢复
Extundelete 数据恢复 “rm -rf /*” 是我们经常使用的命令,操作不慎全盘接蹦,从删库到跑路,身为过来人的我们都经历过rm带来的痛苦. 不要慌,当我们有了Extundelete就可以 ...
- 【题解】Luogu P1471 方差
原题传送门 简单进行推导之后,就能发现很妙的结论 用线段树维护区间和,区间平方和就珂以算出结果 #include <bits/stdc++.h> #define db double #de ...
- python3中argparse模块
1.定义:argparse是python标准库里面用来处理命令行参数的库 2.命令行参数分为位置参数和选项参数: 位置参数就是程序根据该参数出现的位置来确定的 ...
- 查看selenium API
pydoc是Python自带的模块,主要用于从python模块中自动生成文档,这些文档可以基于文本呈现的.也可以生成WEB 页面的,还可以在服务器上以浏览器的方式呈现! 一.pydoc 1.到底什么是 ...
- CSDN去广告插件
因为避免不了与代码打交道,所以经常要上网搜代码,一般搜索到的资源都指向了CSDN,然而,好好的一篇博文,上面有很多广告,看着很不舒服,冲vip是不可能的,穷的的要死,怎么办呢?写个插件把! 去广告原理 ...
- Go 参数传递是传值还是传引用
什么是传值(值传递)? 传值的意思是:函数传递的总是原来这个东西的一个副本.一个副拷贝.比如我们传递一个 int 类型的参数,传递 的其实这个参数的一个副本:传递一个指针类型的参数,其实传递的是这个指 ...
- JAVA中的String类(详解)
Java.lang.String类是final类型的,因此不可以继承这个类.不能修改这个类.String是一个类不属于基本数据类型. 可以从源码中看到,String是一个final类型. String ...
- 简单谈谈$.merge()
var arr1 = [1,2,3]; var arr2 = [1,2,3]; console.log($.merge(arr1,arr2)) //[1,2,3,1,2,3],可见数组间只是合并,不会 ...
- iOS权限申请
<!-- 相册 --> <key>NSPhotoLibraryUsageDescription</key> <string>App需要您的同意,才能访问 ...