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

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

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

企图 使用
can
我可以:

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

new foo.apply(this, myArgs);
我收到此错误(因为我正在调用
new
):

使用
对象。创建

使用
Object.create(proto)
是正确的方法

Coco和LiveScript(Coffeescript子集)提供了一个解决方案:

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(){});
这些黑客行为丑陋、缓慢且不完美;例如,
Date
依赖于其内部
[[PrimitiveValue]]
。看

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(){});