[关闭]
@dudusky 2017-04-07T08:31:17.000000Z 字数 587 阅读 746

箭头函数 arrow function


箭头函数的优势

箭头函数

  1. let add = (a, b) => a + b

箭头函数 VS 普通函数

箭头函数

  1. let add = (a, b) => a + b

普通函数

  1. let add = function(a, b) {
  2. return a + b;
  3. }

  1. () => { ... } // no parameter
  2. x => { ... } // one parameter
  3. (x, y) => { ... } // several parameters

  1. x => x * x // expression
  2. // 等同于
  3. x => { return x * x }
  1. // ES6 箭头函数
  2. const squares = [1, 2, 3].map(x => x * x);
  3. // 普通函数
  4. const squares = [1, 2, 3].map(function (x) { return x * x });

  1. function Prefixer(prefix) {
  2. this.prefix = prefix;
  3. }
  4. Prefixer.prototype.prefixArray = function (arr) {
  5. var that = this; // (A)
  6. return arr.map(function (x) {
  7. return that.prefix + x;
  8. });
  9. };
  10. > var pre = new Prefixer('Hi ');
  11. > pre.prefixArray(['Joe', 'Alex'])
  12. [ 'Hi Joe', 'Hi Alex' ]

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注