How to Create Protected Object Properties in JavaScript

前端 未结 8 1942
梦毁少年i
梦毁少年i 2020-12-10 04:49

Is there a JavaScript pattern which mimics \"Protected\" object properties like what you see in languages like C++ ??

Basically, I\'d like to create an Object A whic

8条回答
  •  北海茫月
    2020-12-10 05:20

    function ClassA(init)
    {
        var protected = {};
        protected.prop = init * 10;
        if(this.constructor != ClassA) { return protected; }
    }
    
    function ClassB()
    {
        var protected = ClassA.call(this, 5); //console.log(protected.prop);
    }
    
    //var a = new ClassA(123);
    //var b = new ClassB();
    

提交回复
热议问题