Accessing “Public” methods from “Private” methods

2019-02-06 11:34发布

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

Check out the class below:

function Class()
{
    this.publicMethod = function()
    {
        alert("hello");
    }

    privateMethod = function()
    {
        publicMethod();
    }

    this.test = function()
    {
        privateMethod();
    }
}

Here is the code I run:

var class = new Class();
class.test();

Firebug gives this error:

publicMethod is not defined: [Break on this error] publicMethod();

Is there some other way to call publicMethod() within privateMethod() without accessing the global class variable [i.e. class.publicMethod()]?

4条回答
迷人小祖宗
2楼-- · 2019-02-06 11:50

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();
  }
}
查看更多
淡お忘
3楼-- · 2019-02-06 11:57

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.

查看更多
再贱就再见
4楼-- · 2019-02-06 11:58

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();
查看更多
狗以群分
5楼-- · 2019-02-06 12:06

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);
    };
}());
查看更多
登录 后发表回答