有关 ES6 箭头函数中“arguments”的官方信息?
Official information on `arguments` in ES6 Arrow functions?
(() => console.log(arguments))(1,2,3);
// Chrome, FF, Node give "1,2,3"
// Babel gives "arguments is not defined" from parent scope
根据 Babel(以及据我所知,最初的 TC39 建议),这是“无效的”,因为箭头函数应该使用它们的父作用域作为参数。我能找到的唯一与此矛盾的信息是一条评论说这被 TC39 拒绝了,但我找不到任何东西来支持它。
只是在这里寻找官方文档。
Chrome,FF,和node这里好像是错的,babel是对的:
箭头函数在其范围内没有自己的 arguments
绑定;调用它们时没有创建参数对象。
looking for official docs here
箭头函数表达式的计算结果为 have their [[ThisMode]] set to lexical
, and when such are called the declaration instantiation does not create an arguments
object 的函数。甚至有一个特定的注释 (18 a) 指出“箭头函数从来没有参数对象。”。
正如 Bergi 所指出的,箭头函数没有自己的 arguments
变量。
但是,如果您确实想为箭头函数捕获参数,您可以简单地使用 rest parameter
const myFunc = (...args) =>
console.log ("arguments", args)
myFunc (1, 2, 3)
// arguments [1, 2, 3]
剩余参数可以与其他位置参数结合使用,但必须始终作为 last 参数包含在内
const myFunc = (a, b, c, ...rest) =>
console.log (a, b, c, rest)
myFunc (1, 2, 3, 4, 5, 6, 7)
// 1 2 3 [ 4, 5, 6, 7 ]
如果你在任何其他位置错误地写了一个rest参数,你会得到一个错误
const myFunc = (...rest, a, b, c) =>
console.log (a, b, c, rest)
myFunc (1, 2, 3, 4, 5, 6, 7)
// Error: Rest parameter must be last formal parameter
(() => console.log(arguments))(1,2,3);
// Chrome, FF, Node give "1,2,3"
// Babel gives "arguments is not defined" from parent scope
根据 Babel(以及据我所知,最初的 TC39 建议),这是“无效的”,因为箭头函数应该使用它们的父作用域作为参数。我能找到的唯一与此矛盾的信息是一条评论说这被 TC39 拒绝了,但我找不到任何东西来支持它。
只是在这里寻找官方文档。
Chrome,FF,和node这里好像是错的,babel是对的:
箭头函数在其范围内没有自己的 arguments
绑定;调用它们时没有创建参数对象。
looking for official docs here
箭头函数表达式的计算结果为 have their [[ThisMode]] set to lexical
, and when such are called the declaration instantiation does not create an arguments
object 的函数。甚至有一个特定的注释 (18 a) 指出“箭头函数从来没有参数对象。”。
正如 Bergi 所指出的,箭头函数没有自己的 arguments
变量。
但是,如果您确实想为箭头函数捕获参数,您可以简单地使用 rest parameter
const myFunc = (...args) =>
console.log ("arguments", args)
myFunc (1, 2, 3)
// arguments [1, 2, 3]
剩余参数可以与其他位置参数结合使用,但必须始终作为 last 参数包含在内
const myFunc = (a, b, c, ...rest) =>
console.log (a, b, c, rest)
myFunc (1, 2, 3, 4, 5, 6, 7)
// 1 2 3 [ 4, 5, 6, 7 ]
如果你在任何其他位置错误地写了一个rest参数,你会得到一个错误
const myFunc = (...rest, a, b, c) =>
console.log (a, b, c, rest)
myFunc (1, 2, 3, 4, 5, 6, 7)
// Error: Rest parameter must be last formal parameter