splat over JavaScript 对象(用 new )?

2023-11-29

如何在不使用的情况下跨越对象ECMA6 特点?

Attempt

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

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

myArgs = [1,2];

With can我只能这样做:

can.apply(this, myArgs);

当尝试与foo:

new foo.apply(this, myArgs);

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

TypeError: function apply() { [native code] } is not a constructor

Using 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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

splat over JavaScript 对象(用 new )? 的相关文章

随机推荐