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


箭头函数
let add = (a, b) => a + b
箭头函数
let add = (a, b) => a + b
普通函数
let add = function(a, b) {return a + b;}
() => { ... } // no parameterx => { ... } // one parameter(x, y) => { ... } // several parameters
x => x * x // expression// 等同于x => { return x * x }
// ES6 箭头函数const squares = [1, 2, 3].map(x => x * x);// 普通函数const squares = [1, 2, 3].map(function (x) { return x * x });
没有this绑定
that = this
function Prefixer(prefix) {this.prefix = prefix;}Prefixer.prototype.prefixArray = function (arr) {var that = this; // (A)return arr.map(function (x) {return that.prefix + x;});};> var pre = new Prefixer('Hi ');> pre.prefixArray(['Joe', 'Alex'])[ 'Hi Joe', 'Hi Alex' ]