Getter/setter in constructor

前端 未结 6 1274
清酒与你
清酒与你 2020-12-12 20:43

I recently read about the fact that there is a possibility of defining getters/setters in JavaScript. It seems extremely helpful - the setter is a kind of \'helper\' which c

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-12 21:30

    function Obj(value){
        this.value = !!value;
    }
    
    Obj.prototype = {
        get test () {
            return this.value;``
        },
        set test (value) {
            this.value = !!this.value;
        }
    };
    var obj = new Obj(true);
    

提交回复
热议问题