Arrow functions

Arrow functions表达式相比函数表达式有更短的语法,没有自己的this、argument、super或者new.target。

1.语法规则:

基础语法:

  (param1, param2, …, paramN) => { statements }

  (param1, param2, …, paramN) => expression

  // equivalent to: => { return expression; }

  // Parentheses are optional when there's only one parameter name:

  (singleParam) => { statements }

  singleParam => { statements }

  // The parameter list for a function with no parameters should be written with a pair of parentheses.

  () => { statements }

高级语法:

  // Parenthesize the body of function to return an object literal expression:

  params => ({foo: bar})

  // Rest parameters and default parameters are supported

  (param1, param2, ...rest) => { statements }

  (param1 = defaultValue1, param2, …, paramN = defaultValueN) => {

  statements }

  // Destructuring within the parameter list is also supported

  var f = ([a, b] = [1, 2], {x: c} = {x: a + b}) => a + b + c;

  f(); // 6

2.箭头函数用作方法

箭头函数表达式最适合非方法函数

Eg:

  'use strict';

  var obj = {

  i: 10,

  b: () => console.log(this.i, this),

  c: function() {

     console.log(this.i, this);

  }

  }

  obj.b(); // prints undefined, Window {...} (or the global object)

  obj.c(); // prints 10, Object {...}

3.功能体

箭头功能可以具有“简洁的身体”或通常的“块体”。

在简洁的主体中,只指定了一个表达式,该表达式成为显式返回值。在块体中,您必须使用显式return语句。

Eg:

  var func = x => x * x;

  // concise body syntax, implied "return"

  var func = (x, y) => { return x + y; };

  // with block body, explicit "return" needed

虽然箭头函数中的箭头不是运算符,但箭头函数具有特殊的解析规则,与常规函数相比,它们与运算符优先级的交互方式不同。

  let callback;

  

  callback = callback || function() {}; // ok

  callback = callback || () => {};

  // SyntaxError: invalid arrow-function arguments

  callback = callback || (() => {});    // ok

----Arrow functions----的更多相关文章

  1. 《理解 ES6》阅读整理:函数(Functions)(四)Arrow Functions

    箭头函数(Arrow Functions) 就像名字所说那样,箭头函数使用箭头(=>)来定义函数.与传统函数相比,箭头函数在多个地方表现不一样. 箭头函数语法(Arrow Function Sy ...

  2. JavaScript ES6 Arrow Functions(箭头函数)

    1. 介绍 第一眼看到ES6新增加的 arrow function 时,感觉非常像 lambda 表达式. 那么arrow function是干什么的呢?可以看作为匿名函数的简写方式. 如: var ...

  3. ES6 In Depth: Arrow functions

    Arrows <script language="javascript"> <!-- document.bgColor = "brown"; ...

  4. ES6学习笔记<二>arrow functions 箭头函数、template string、destructuring

    接着上一篇的说. arrow functions 箭头函数 => 更便捷的函数声明 document.getElementById("click_1").onclick = ...

  5. Arrow functions and the ‘this’ keyword

    原文:https://medium.freecodecamp.org/learn-es6-the-dope-way-part-ii-arrow-functions-and-the-this-keywo ...

  6. ES6 箭头函数(Arrow Functions)

    ES6 箭头函数(Arrow Functions) ES6 可以使用 "箭头"(=>)定义函数,注意是函数,不要使用这种方式定义类(构造器). 一.语法 具有一个参数的简单函 ...

  7. arrow functions 箭头函数

    ES6里新增加的,与普通方法不同的地方 1.this 的对象在定义函数的时候确定了,而不是在使用的时候才决定 2.不可以使用 new  ,也就不能当构造函数 3.this 的值一旦确定无法修改     ...

  8. 箭头函数 Arrow Functions/////////////////////zzz

    箭头符号在JavaScript诞生时就已经存在,当初第一个JavaScript教程曾建议在HTML注释内包裹行内脚本,这样可以避免不支持JS的浏览器误将JS代码显示为文本.你会写这样的代码: < ...

  9. ES6箭头函数(Arrow Functions)

    ES6可以使用“箭头”(=>)定义函数,注意是函数,不要使用这种方式定义类(构造器). 一.语法 1. 具有一个参数的简单函数 var single = a => a single('he ...

  10. 深入浅出ES6(七):箭头函数 Arrow Functions

    作者 Jason Orendorff  github主页  https://github.com/jorendorff 箭头符号在JavaScript诞生时就已经存在,当初第一个JavaScript教 ...

随机推荐

  1. c#switch语句的用法

    switch条件语句是一种很常用的选择语句,它与if条件语句不同,它只针对某个表达式的值作出判断,从而决定执行哪一段代码. switch条件语句用到的关键字: switch case break de ...

  2. python初学心得之一

    昨天开始接触并学习python,对python有了初步印象. 一.python主要应用方向 二.python语言类型 三.python2和3的主要区别 四.常见字符编码 五.Python语法初学  一 ...

  3. groovy 知识集锦

    对应官方的<Program structure>的中文翻译 http://www.cnblogs.com/zhaoxia0815/p/7404387.html

  4. .net core 2.0 HTTPS request fails using HttpClient 安全错误

    最近.net core 项目中遇到一个问题,通过Httpclient 访问https的接口报错,错误如下: WinHttpException: A security error occurred Sy ...

  5. SQL Server死锁诊断--同一行数据在不同索引操作下引起的死锁

    死锁概述 对于数据库中出现的死锁,通俗地解释就是:不同Session(会话)持有一部分资源,并且同时相互排他性地申请对方持有的资源,然后双方都得不到自己想要的资源,从而造成的一种僵持的现象.当然,在任 ...

  6. 基于Ubuntu的ESP32平台搭建

    提要:针对于Ubuntu下的ESP32搭建,网上有很多博文,乐鑫官网也有指导手册,对于到家都知道的部分我就一带而过,我主要描述搭建过程中遇到的问题和细节. 1.创建一个ESP的目录 I)在家目录下创建 ...

  7. shardingsphere多数据源(springboot + mybatis+shardingsphere+druid)

    org.springframeword.boot:spring-boot-starer-web: 2.0.4release io.shardingsphere:sharding-jdbc-spring ...

  8. 微信小程序---获取上传图片大小

    wx.chooseImage({ count: 1, sizeType: ['compressed'], sourceType: ['album', 'camera'], success: funct ...

  9. soa 和微服务的区别

    soa beased applications are compromised of more loosely coupled componets that use an enterprise ser ...

  10. Form-encoded method must contain at least one @Field.

    https://blog.csdn.net/liunian823/article/details/80290855 记得之前遇到过这个问题,并且记录笔记了,这次再翻笔记,却没有找到...搜索 了下. ...