【发布时间】:2018-11-26 21:17:46
【问题描述】:
我想了解普通函数与箭头函数的行为。
箭头功能:
function arrowFunc() {
return () => arguments
}
console.log(arrowFunc(1, 2, 3)(1))
正态函数
function normalFunc() {
return function() {
return arguments
}
}
console.log(normalFunc(1, 2, 3)(1))
两个结果都应该是相同的,但看起来上面定义的 arrowFunc 考虑了第一个 arg 列表,而 normalFunc 考虑了第二组 arg 列表。
也尝试了 babel-compilation 来理解差异,但看起来行为不同,如下所示:
Babel 输出:
"use strict";
function arrowFunc() {
var _arguments = arguments;
return function() {
return _arguments;
};
}
console.log(arrowFunc(1, 2, 3)(1));
function normalFunc() {
return function() {
return arguments;
};
}
console.log(normalFunc(1, 2, 3)(1));
【问题讨论】:
-
Both the results are expected to be same不,箭头函数没有arguments。就像this,如果存在外部arguments,它只会引用外部绑定
标签: javascript arrow-functions