Why doesn't JavaScript ES6 support multi-constructor classes?

前端 未结 8 2164
终归单人心
终归单人心 2020-12-03 04:25

I want to write my Javascript class like below.

class Option {
    constructor() {
        this.autoLoad = false;
    }

    constructor(key, value) {
               


        
8条回答
  •  渐次进展
    2020-12-03 04:39

    you can use static methods,look at my answer to same question

    class MyClass {
        constructor(a,b,c,d){
            this.a = a
            this.b = b
            this.c = c
            this.d = d
        }
        static BAndCInstance(b,c){
            return new MyClass(null,b,c)
        }
    }
    
    //a Instance that has b and c params
    MyClass.BAndCInstance(b,c)
    

提交回复
热议问题