Accessing “Public” methods from “Private” methods in javascript class

后端 未结 4 2076
谎友^
谎友^ 2020-12-31 09:45

Is there a way to call \"public\" javascript functions from \"private\" ones within a class?

Check out the class below:

function Class()
{
    this.p         


        
4条回答
  •  离开以前
    2020-12-31 10:38

    Is this approach not a advisable one? I am not sure though

    var klass = function(){
      var privateMethod = function(){
        this.publicMethod1();
      }.bind(this);
    
      this.publicMethod1 = function(){
        console.log("public method called through private method");
      }
    
      this.publicMethod2 = function(){
        privateMethod();
      }
    }
    
    var klassObj = new klass();
    klassObj.publicMethod2();
    

提交回复
热议问题