函数rest参数抛出错误

function rest parameters throw error

本文关键字:出错 错误 参数 rest 函数      更新时间:2023-09-26

正在运行:

$> node restparamstest.js

其中:

restparamstest.js

var addTestNotification = function(x, ...theArgs) {
theArgs.forEach(function (post) {
    console.log(post);
});
};
addTestNotification(1, 2, 4);

投掷:

(function (exports, require, module, __filename, __dirname) { var addTestNotification = function(x, ...theArgs) {
                                                                                                    ^^^
SyntaxError: Unexpected token ...
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:968:3

节点版本:

console.log(process.versions);
{ http_parser: '2.5.2',
  node: '4.4.7',
  v8: '4.5.103.36',

有什么想法吗?谢谢

Rest参数只有在节点6.31之后才完全支持。如果要将它们与早期版本的节点一起使用,则应使用--harmony标志。

您可以在此处查看ES2015对节点版本的支持。

试试这个

node --harmony restparamstest.js
相关文章: