Javascript在同一个对象中从私有方法调用公共方法

时间:2021-08-31 16:45:15

Can I call public method from within private one:

我可以在私人方式中调用公共方法:

var myObject = function() {
   var p = 'private var';
   function private_method1() {
      //  can I call public method "public_method1" from this(private_method1) one and if yes HOW?
   }

   return {
      public_method1: function() {
         // do stuff here
      }
   };
} ();

5 个解决方案

#1


do something like:

做类似的事情:

var myObject = function() {
   var p = 'private var';
   function private_method1() {
      public.public_method1()
   }

   var public = {
      public_method1: function() {
         alert('do stuff')
      },
      public_method2: function() {
         private_method1()
      }
   };
   return public;
} ();
//...

myObject.public_method2()

#2


Why not do this as something you can instantiate?

为什么不把它作为你可以实例化的东西呢?

function Whatever()
{
  var p = 'private var';
  var self = this;

  function private_method1()
  {
     // I can read the public method
     self.public_method1();
  }

  this.public_method1 = function()
  {
    // And both test() I can read the private members
    alert( p );
  }

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

var myObject = new Whatever();
myObject.test();

#3


public_method1 is not a public method. It is a method on an anonymous object that is constructed entirely within the return statement of your constructor function.

public_method1不是公共方法。它是匿名对象上的一个方法,它完全在构造函数的return语句中构造。

If you want to call it, why not structure the object like this:

如果你想调用它,为什么不像这样构造对象:

var myObject = function() {
    var p...
    function private_method() {
       another_object.public_method1()
    }
    var another_object = { 
        public_method1: function() {
            ....
        }
    }
    return another_object;
}() ;

#4


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


Do not know direct answer, but following should work.

不知道直接答案,但以下应该工作。

var myObject = function() 
{
   var p = 'private var';   
  function private_method1() {
   _public_method1()
  }
  var _public_method1 =  function() {
         // do stuff here
    }

  return {
    public_method1: _public_method1
  };
} ();

#1


do something like:

做类似的事情:

var myObject = function() {
   var p = 'private var';
   function private_method1() {
      public.public_method1()
   }

   var public = {
      public_method1: function() {
         alert('do stuff')
      },
      public_method2: function() {
         private_method1()
      }
   };
   return public;
} ();
//...

myObject.public_method2()

#2


Why not do this as something you can instantiate?

为什么不把它作为你可以实例化的东西呢?

function Whatever()
{
  var p = 'private var';
  var self = this;

  function private_method1()
  {
     // I can read the public method
     self.public_method1();
  }

  this.public_method1 = function()
  {
    // And both test() I can read the private members
    alert( p );
  }

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

var myObject = new Whatever();
myObject.test();

#3


public_method1 is not a public method. It is a method on an anonymous object that is constructed entirely within the return statement of your constructor function.

public_method1不是公共方法。它是匿名对象上的一个方法,它完全在构造函数的return语句中构造。

If you want to call it, why not structure the object like this:

如果你想调用它,为什么不像这样构造对象:

var myObject = function() {
    var p...
    function private_method() {
       another_object.public_method1()
    }
    var another_object = { 
        public_method1: function() {
            ....
        }
    }
    return another_object;
}() ;

#4


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


Do not know direct answer, but following should work.

不知道直接答案,但以下应该工作。

var myObject = function() 
{
   var p = 'private var';   
  function private_method1() {
   _public_method1()
  }
  var _public_method1 =  function() {
         // do stuff here
    }

  return {
    public_method1: _public_method1
  };
} ();