【问题标题】:Heroku not compatible with rest parameter ...args [duplicate]Heroku与其余参数不兼容...args [重复]
【发布时间】:2018-11-08 05:12:11
【问题描述】:
Heroku 说:SyntaxError: Unexpected token ...
如何调整此功能以兼容 heroku?
authenticate(...args) {
var authRequest = {};
authRequest[Eureca.Protocol.authReq] = args;
this.socket.send(this.serialize(authRequest));
}
【问题讨论】:
标签:
javascript
node.js
heroku
【解决方案1】:
我对 Heroku 不是很熟悉,但您总是可以执行以下操作。 Array.prototype.slice.call(arguments) 在这种情况下会达到与使用 rest 运算符相同的效果。
authenticate() {
const args = Array.prototype.slice.call(arguments);
var authRequest = {};
authRequest[Eureca.Protocol.authReq] = args;
this.socket.send(this.serialize(authRequest));
}
查看示例。
function foo1(...args) {
console.log(args);
}
function foo2() {
const args = Array.prototype.slice.call(arguments);
console.log(args);
}
foo1(1, 2, 3, 4);
foo2(1, 2, 3, 4);
而这段代码是通过babel transpiler运行foo1函数得到的。
function foo1() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
console.log(args);
}