JavaScript对象上的splat(使用新的)?

JavaScript对象上的splat(使用新的)?,javascript,arguments,argument-passing,ecmascript-5,splat,Javascript,Arguments,Argument Passing,Ecmascript 5,Splat,如何在不使用的情况下在对象之间展开 企图 我能做什么 can.apply(this, myArgs); 尝试使用foo时: 我收到此错误是因为我正在调用new: 使用Object.create 使用Object.createproto是正确的方法 Coco和LiveScript咖啡脚本子集提供了一个解决方案: new foo ...args 编译成 (function(func, args, ctor) { ctor.prototype = func.prototype; var c

如何在不使用的情况下在对象之间展开

企图 我能做什么

can.apply(this, myArgs);
尝试使用foo时:

我收到此错误是因为我正在调用new:

使用Object.create 使用Object.createproto是正确的方法

Coco和LiveScript咖啡脚本子集提供了一个解决方案:

new foo ...args
编译成

(function(func, args, ctor) {
  ctor.prototype = func.prototype;
  var child = new ctor, result = func.apply(child, args), t;
  return (t = typeof result)  == "object" || t == "function" ? result || child : child;
  })
(foo, args, function(){});
在《咖啡脚本》中:

(function(func, args, ctor) {
  ctor.prototype = func.prototype;
  var child = new ctor, result = func.apply(child, args);
  return Object(result) === result ? result : child;
})(foo, args, function(){});
这些黑客行为丑陋、缓慢且不完美;例如,日期依赖于其内部[[PrimitiveValue]]。看

function foo(bar, haz) {
    this.bar = bar;
    this.haz = haz;
}

x = Object.create(foo.prototype);
myArgs = [5,6];
foo.apply(x, myArgs);

console.log(x.bar);
new foo ...args
(function(func, args, ctor) {
  ctor.prototype = func.prototype;
  var child = new ctor, result = func.apply(child, args), t;
  return (t = typeof result)  == "object" || t == "function" ? result || child : child;
  })
(foo, args, function(){});
(function(func, args, ctor) {
  ctor.prototype = func.prototype;
  var child = new ctor, result = func.apply(child, args);
  return Object(result) === result ? result : child;
})(foo, args, function(){});