Javascript 函数vs胖箭头
因此,如果我们以这个Javascript 函数vs胖箭头,javascript,function,oop,callback,Javascript,Function,Oop,Callback,因此,如果我们以这个函数为例: function executeCallback (callback) { callback(); } 现在,不管我放置一个胖箭头还是一个正常的函数,两者都可以工作,如下所示: executeCallback(function () { alert('This is a function'); }) executeCallback(() => { alert('This is a fat arrow function'); }) 所以,除了
函数
为例:
function executeCallback (callback) {
callback();
}
现在,不管我放置一个胖箭头
还是一个正常的函数
,两者都可以工作,如下所示:
executeCallback(function () {
alert('This is a function');
})
executeCallback(() => {
alert('This is a fat arrow function');
})
所以,除了较短的语法。两者之间的区别到底是什么?它如何影响面向对象编程?有关更深入的信息,请参阅上的此答案。还有,看看这个 有几个原因
更直观地处理当前对象上下文。 ES6箭头
this.nums.forEach((v) => {
if (v % 5 === 0)
this.fives.push(v) //this actually points to the context of the callback
});
Ecmascript 5
// variant 1
var self = this;
this.nums.forEach(function (v) {
if (v % 5 === 0)
self.fives.push(v);
});
// variant 2
this.nums.forEach(function (v) {
if (v % 5 === 0)
this.fives.push(v);
}, this);
// variant 3 (since ECMAScript 5.1 only)
this.nums.forEach(function (v) {
if (v % 5 === 0)
this.fives.push(v);
}.bind(this));
odds = evens.map(function (v) { return v + 1; });
pairs = evens.map(function (v) { return { even: v, odd: v + 1 }; });
nums = evens.map(function (v, i) { return v + i; });
更具表现力的闭包语法。
ES6箭头
this.nums.forEach((v) => {
if (v % 5 === 0)
this.fives.push(v) //this actually points to the context of the callback
});
赔率=evens.map(v=>v+1)
pairs=evens.map(v=>({偶数:v,奇数:v+1}))
nums=evens.map((v,i)=>v+i)
Ecmascript 5
// variant 1
var self = this;
this.nums.forEach(function (v) {
if (v % 5 === 0)
self.fives.push(v);
});
// variant 2
this.nums.forEach(function (v) {
if (v % 5 === 0)
this.fives.push(v);
}, this);
// variant 3 (since ECMAScript 5.1 only)
this.nums.forEach(function (v) {
if (v % 5 === 0)
this.fives.push(v);
}.bind(this));
odds = evens.map(function (v) { return v + 1; });
pairs = evens.map(function (v) { return { even: v, odd: v + 1 }; });
nums = evens.map(function (v, i) { return v + i; });
有关详细信息,请参阅上的此答案。还有,看看这个 有几个原因
更直观地处理当前对象上下文。 ES6箭头
this.nums.forEach((v) => {
if (v % 5 === 0)
this.fives.push(v) //this actually points to the context of the callback
});
Ecmascript 5
// variant 1
var self = this;
this.nums.forEach(function (v) {
if (v % 5 === 0)
self.fives.push(v);
});
// variant 2
this.nums.forEach(function (v) {
if (v % 5 === 0)
this.fives.push(v);
}, this);
// variant 3 (since ECMAScript 5.1 only)
this.nums.forEach(function (v) {
if (v % 5 === 0)
this.fives.push(v);
}.bind(this));
odds = evens.map(function (v) { return v + 1; });
pairs = evens.map(function (v) { return { even: v, odd: v + 1 }; });
nums = evens.map(function (v, i) { return v + i; });
更具表现力的闭包语法。
ES6箭头
this.nums.forEach((v) => {
if (v % 5 === 0)
this.fives.push(v) //this actually points to the context of the callback
});
赔率=evens.map(v=>v+1)
pairs=evens.map(v=>({偶数:v,奇数:v+1}))
nums=evens.map((v,i)=>v+i)
Ecmascript 5
// variant 1
var self = this;
this.nums.forEach(function (v) {
if (v % 5 === 0)
self.fives.push(v);
});
// variant 2
this.nums.forEach(function (v) {
if (v % 5 === 0)
this.fives.push(v);
}, this);
// variant 3 (since ECMAScript 5.1 only)
this.nums.forEach(function (v) {
if (v % 5 === 0)
this.fives.push(v);
}.bind(this));
odds = evens.map(function (v) { return v + 1; });
pairs = evens.map(function (v) { return { even: v, odd: v + 1 }; });
nums = evens.map(function (v, i) { return v + i; });
- arrow函数自动将当前上下文绑定到this,因此您无需执行
myfunction.bind(this,args)
- arrow函数自动将当前上下文绑定到this,因此您无需执行
myfunction.bind(this,args)
返回
我确实注意到了这一点。但这是两者之间唯一的区别吗?这个
的绑定方式根本不同哦,还有一个隐式的返回
我确实注意到了这一点。但这是两者之间唯一的区别吗?