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. 201772020113 李清华《面向对象程序设计(java)》第16周学习总结

    1.实验目的与要求 (1) 掌握线程概念: (2) 掌握线程创建的两种技术: (3) 理解和掌握线程的优先级属性及调度方法: (4) 掌握线程同步的概念及实现技术: 2.实验内容和步骤 实验1:测试程 ...

  2. node读取文件转换json文件

    { ".323":"text/h323" , ".3gp":"video/3gpp" , ".aab" ...

  3. How to Build a New Habit: This is Your Strategy Guide

    How to Build a New Habit: This is Your Strategy Guide by James ClearRead this on JamesClear.com Acco ...

  4. anu小程序快速入门

    众所周知,微信推出小程序以来,可谓火遍大江南北,就像当前互联网兴起时,大家忙着抢域名与开私人博客一样.小程序之所以这么火,是因为微信拥有庞大的用户量,并且腾讯帮你搞定后台问题及众多功能问题(如分享,支 ...

  5. Redis物理文件结构

    Redis物理文件结构  对于数据库,个人习惯先从物理结构文件入手,整体上看一下有哪些文件,都是什么作用.类似于MySQL,数据文件和配置文件是Redis最基本也是做主要的两个物理文件之一,相比MyS ...

  6. hbase 存储结构和原理

    HBase的表结构 建表时要指定的是:表名.列族 建表语句 create 'user_info', 'base_info', 'ext_info' 意思是新建一个表,名称是user_info,包含两个 ...

  7. c# linq 汇总

    例子:List<string> list = new List<string>(); list.Add("1 a");list.Add("2 b& ...

  8. CSS 图像高级 Css Sprites

    上节课中我们学习了背景图像,这节课我们学习背景图像的高级知识,如Css Sprites,CSS 背景渐变等. Css Sprites Css Sprites,国内也叫CSS精灵.它的原理是将许多的小图 ...

  9. python day08 文件操作

    2019.4.8 S21 day08笔记总结 一.进制 对于计算机而言无论是文件存储 / 网络传输数据本质上都是:二进制(010101010101),如:电脑上存储视频/图片/文件都是二进制: QQ/ ...

  10. 网页html格式导出Excel.xls

    1.创建excel方法 /// <summary> /// 创建Excel表格 /// </summary> /// <param name="dt" ...