10th week task -3 Arrow function restore
为什么叫Arrow Function?因为它的定义用的就是一个箭头:
x => x * x
上面的箭头函数相当于:
function (x) {
return x * x;
}
箭头函数相当于匿名函数,并且简化了函数定义。箭头函数有两种格式,一种像上面的,只包含一个表达式,连{ ... }和return都省略掉了。还有一种可以包含多条语句,这时候就不能省略{ ... }和return:
x => {
if (x > 0) {
return x * x;
}
else {
return - x * x;
}
}
如果参数不是一个,就需要用括号()括起来:
// 两个参数:
(x, y) => x * x + y * y
// 无参数:
() => 3.14
// 可变参数:
(x, y, ...rest) => {
var i, sum = x + y;
for (i=0; i<rest.length; i++) {
sum += rest[i];
}
return sum;
}
如果要返回一个对象,就要注意,如果是单表达式,这么写的话会报错:
// SyntaxError:
x => { foo: x }
因为和函数体的{ ... }有语法冲突,所以要改为:
// ok:
x => ({ foo: x })
var materials = [
'Hydrogen',
'Helium',
'Lithium',
'Beryllium'
];
console.log(materials.map(material => material.length));
写出这个的标准函数形式,就像下面这个形式一样
var selected = allJobs.filter(function (job) {
return job.isSelected();
});
var selected = allJobs.filter(job => job.isSelected());
10th week task -3 Arrow function restore的更多相关文章
- Arrow function restore
Arrow function restore 为什么叫Arrow Function?因为它的定义用的就是一个箭头: x => x * x 上面的箭头函数相当于: function (x) { r ...
- 廖雪峰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 ...
- arrow function
简介 JavaScript 中,函数可以用箭头语法(”=>”)定义,有时候也叫“lambda表达式”.这种语法主要意图是定义轻量级的内联回调函数.例如: // Arrow function: [ ...
- 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 ...
- [ES6] 06. Arrow Function =>
ES6 arrow function is somehow like CoffeeScirpt. CoffeeScript: //function call coffee = -> coffee ...
随机推荐
- springboot配置文件的所有属性
转载:https://blog.csdn.net/qq_28929589/article/details/79439795 # spring boot application.properties配置 ...
- composer 创建自己包
服务器环境下创建自己的项目文件 初始化composer 打开cmd 窗口,cd 到 backrestore 执行 composer init 命令 D:\phpStudy\WWW\backrestor ...
- R语言学习笔记(四)
6. 数据转换 本章主要讲述apply系列函数:apply.lapply.sapply.tapply.mapply,以及姊妹函数by.split.适用于批量处理数据,而不许循环. 6.1 向量分组 用 ...
- 利用Python将文件进行分类整理
利用Python将文件进行分类整理 功能 根据一个文件夹中的文件类型建立相应的文件夹,将同一种类型的文件放在一个文件夹中. 实现思路 主要用到 os 和 shutil 两个库,os 用来获取文件夹中的 ...
- myeclipse启动后,卡在loading workbench界面
今天在修改svn的配置文件的时候,电脑重启了几次,然后myeclipse启动的时候就一直卡在loading workbeach动不了了. 重启了几次也不行,后来查的百度,找到的解决方法如下: 找到my ...
- MNIST数据集分类简单版本
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #载入数据集 mnist = ...
- Emit生成特定接口的类
参考 动态生成类 http://www w2bc com/Article/44799 http://www.cnblogs.com/yingql/archive/2009/03/24/1420914. ...
- linux 工具(1)------终端提示符配置
Linux环境变量,PS1用于设置终端的提示符. 设置规则 设置方法 设置规则 \d :代表日期,格式为 Weekday Month Date,例如 "Mon Aug 1" \H ...
- Maven-常规问题
1.编译(compile)Maven项目时,报错: -Dmaven.multiModuleProjectDirectory system property is not set. 处理方案:Windo ...
- 024-cxf.xml配置文件模板
版本一 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://ww ...