splat over JavaScript 对象(用 new )?

A T*_*A T 5 javascript arguments argument-passing splat ecmascript-5

如何在不使用ECMA6 功能的情况下跨对象进行喷射?

试图

function can(arg0, arg1) {
    return arg0 + arg1;
}

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

myArgs = [1,2];
Run Code Online (Sandbox Code Playgroud)

我可以这样can做:

can.apply(this, myArgs);
Run Code Online (Sandbox Code Playgroud)

尝试使用时foo

new foo.apply(this, myArgs);
Run Code Online (Sandbox Code Playgroud)

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

TypeError: function apply() { [native code] } is not a constructor
Run Code Online (Sandbox Code Playgroud)

A T*_*A T 5

使用Object.create

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);
Run Code Online (Sandbox Code Playgroud)