splat over JavaScript object (with new)?

前端 未结 2 952
感动是毒
感动是毒 2021-01-19 10:14

How do I splat across objects without using ECMA6 features?

Attempt

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

function foo(bar, haz) {
           


        
2条回答
  •  清歌不尽
    2021-01-19 11:01

    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);
    

提交回复
热议问题