How can I construct an object using an array of values for parameters, rather than listing them out, in JavaScript?

前端 未结 5 505

Is this possible? I am creating a single base factory function to drive factories of different types (but have some similarities) and I want to be able to pass arguments as

5条回答
  •  没有蜡笔的小新
    2020-12-29 12:41

    what about a workaround?

    function MyClass(arg1, arg2) {
    
        this.init = function(arg1, arg2){
            //if(arg1 and arg2 not null) do stuff with args
        }
    
        init(arg1, arg2);
    }
    

    So how you can:

    var obj = new MyClass();
    obj.apply(obj, args);
    

提交回复
热议问题