8. 箭头函数

  "use strict"

function Prefixer(prefix) {
  this.prefix = prefix;
}

Prefixer.prototype.prefixArray  = function (arr) {
  let that = this;
  return arr.map(function (x) {
    console.log(that.prefix + x);
  });
};


// es6 写法
Prefixer.prototype.prefixArray = function (arr) {
  return arr.map((x) => {
    console.log(this.prefix + x);
  })
};

let pre = new Prefixer('hello ');
pre.prefixArray(['Brand', 'John']);


let add = function (a, b) {
  let sum = a + b;
  console.log(sum);
  return false;
}

// es6 写法
let add = (a, b) {
  let sum = a + b;
  console.log(sum);
  return false;
}

1.箭头函数里的this, 绑定到了父函数。

posted @ 2017-03-12 22:21  涵叔  阅读(112)  评论(0)    收藏  举报