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, 绑定到了父函数。

浙公网安备 33010602011771号