简体   繁体   English

从javascript类中的“私有”方法访问“公共”方法

[英]Accessing “Public” methods from “Private” methods in javascript class

Is there a way to call "public" javascript functions from "private" ones within a class? 有没有办法从类中的“私有”函数调用“公共”javascript函数?

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: Firebug给出了这个错误:

publicMethod is not defined: [Break on this error] publicMethod(); publicMethod未定义:[中断此错误] publicMethod();

Is there some other way to call publicMethod() within privateMethod() without accessing the global class variable [ie class.publicMethod()]? 是否有其他方法可以在privateMethod()中调用publicMethod()而无需访问全局类变量[即class.publicMethod()]?

You can save off a variable in the scope of the constructor to hold a reference to this . 您可以保存构造函数范围内的变量以保存this的引用。

Please Note: In your example, you left out var before privateMethod = function() making that privateMethod global. 请注意:在您的示例中,您在privateMethod = function()之前privateMethod = function()var ,使privateMethod全局化。 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();
  }
}

The accepted answer has the possibly undesirable side effect that separate copies of publicMethod , test , and privateMethod will be created in each instance. 接受的答案可能会产生不良副作用,即在每个实例中都会创建单独的publicMethodtestprivateMethod副本。 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. 换句话说,您需要this函数作为参数传递给私有函数。 In return, you get a true prototype without having to pollute each instance with its own versions of the private and public functions. 作为回报,您将获得一个真正的原型,而不必使用自己的私有和公共函数版本污染每个实例。

torazaburo's answer is the best one, as it avoids creation of multiple copies of the private members. torazaburo的答案是最好的,因为它避免创建私人成员的多个副本。 I'm surprised that Crockford doesn't mention it at all. 我很惊讶Crockford根本没有提到它。 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);
    };
}());

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM