繁体   English   中英

函数剩余参数抛出错误

[英]function rest parameters throw error

运行:

$> 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',

有任何想法吗? 谢谢!

从节点6.31开始,完全支持rest参数。 如果要在早期版本的节点中使用它们,则应使用--harmony标志。

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

试试这个

node --harmony restparamstest.js

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM