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

后端 未结 4 2075
谎友^
谎友^ 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();
    
    0 讨论(0)
  • 2020-12-31 10:42

    torazaburo's answer is the best one, as it avoids creation of multiple copies of the private members. I'm surprised that Crockford doesn't mention it at all. Alternately, depending on the syntax you prefer for declaring public member functions, you could do this:

    function Class()
    {}
    
    (function() {
        var privateMethod = function(self) {
            self.publicMethod();
        };
    
        Class.prototype.publicMethod = function() {
            alert('hello');
        };
    
        Class.prototype.test = function() {
            privateMethod(this);
        };
    }());
    
    0 讨论(0)
  • 2020-12-31 10:44

    You can save off a variable in the scope of the constructor to hold a reference to this.

    Please Note: In your example, you left out var before privateMethod = function() making that privateMethod global. I have updated the solution here:

    function Class()
    {
      // store this for later.
      var self = this;
      this.publicMethod = function()
      {
        alert("hello");
      }
    
      var privateMethod = function()
      {
        // call the method on the version we saved in the constructor
        self.publicMethod();
      }
    
      this.test = function()
      {
        privateMethod();
      }
    }
    
    0 讨论(0)
  • 2020-12-31 10:48

    The accepted answer has the possibly undesirable side effect that separate copies of publicMethod, test, and privateMethod will be created in each instance. The idiom for avoiding this is:

    function Class()
    {}
    
    Class.prototype=(function()
    {
        var privateMethod = function(self)
        {
            self.publicMethod();
        }
    
    
        return 
        {
            publicMethod: function()
            {
                alert("hello");
            },
            test: function()
            {
                privateMethod(this);
            }
        };
    }());
    

    In other words, you need to pass the this to the private function as an argument. In return, you get a true prototype without having to pollute each instance with its own versions of the private and public functions.

    0 讨论(0)
提交回复
热议问题