JavaScript private methods

前端 未结 30 1470
-上瘾入骨i
-上瘾入骨i 2020-11-22 08:16

To make a JavaScript class with a public method I\'d do something like:

function Restaurant() {}

Restaurant.prototype.buy_food = function(){
   // something         


        
30条回答
  •  隐瞒了意图╮
    2020-11-22 08:45

    If you want the full range of public and private functions with the ability for public functions to access private functions, layout code for an object like this:

    function MyObject(arg1, arg2, ...) {
      //constructor code using constructor arguments...
      //create/access public variables as 
      // this.var1 = foo;
    
      //private variables
    
      var v1;
      var v2;
    
      //private functions
      function privateOne() {
      }
    
      function privateTwon() {
      }
    
      //public functions
    
      MyObject.prototype.publicOne = function () {
      };
    
      MyObject.prototype.publicTwo = function () {
      };
    }
    

提交回复
热议问题