[removed] How to create a new instance of a class without using the new keyword?

后端 未结 6 2216
死守一世寂寞
死守一世寂寞 2020-12-25 13:22

I think the following code will make the question clear.

// My class
var Class = function() { console.log(\"Constructor\"); };
Class.prototype = { method: fu         


        
6条回答
  •  -上瘾入骨i
    2020-12-25 13:49

    I guess browser independent solution would be better

    function empty() {}
    
    function factory(clazz /*, some more arguments for constructor */) {
        empty.prototype = clazz.prototype;
        var obj = new empty();
        clazz.apply(obj, Array.prototype.slice.call(arguments, 1));
        return obj;
    }
    

提交回复
热议问题