JavaScript private methods

Keywords: Javascript

To create a JavaScript class using public methods, I need to do the following:

function Restaurant() {}

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

Restaurant.prototype.use_restroom = function(){
   // something here
}

In this way, users of our class can:

var restaurant = new Restaurant();
restaurant.buy_food();
restaurant.use_restroom();

How to create private methods that can be called by the buy u food and use U restroom methods but not by users of this class?

In other words, I want my approach implementation to:

Restaurant.prototype.use_restroom = function() {
   this.private_stuff();
}

But it doesn't work:

var r = new Restaurant();
r.private_stuff();

How to define private "stuff" as a private method so that both of them are true?

I've read it Doug Crockford's article Several times, but it seems that the public method cannot call the private method, while the external method can call the privileged method.

#1 building

Because everyone here publishes their own code, so I will do the same

I like Crockford because he introduces real object-oriented patterns into Javascript. But he also raised a new misconception, "that.".

So why does he use "that = this"? It has nothing to do with private functions. It has to do with internal functions!

Because according to Crockford, this is the wrong code:

Function Foo( ) {
    this.bar = 0; 
    var foobar=function( ) {
        alert(this.bar);
    }
} 

He therefore suggested that:

Function Foo( ) {
    this.bar = 0;
    that = this; 
    var foobar=function( ) {
        alert(that.bar);
    }
}

So, as I said, I'm sure Crockford's explanation for him is wrong, but his code is definitely right. Or is he just spoofing the Javascript world to see who's copying his code? I don't know... I'm not a browser geek; D

edit

Well, that's all: 'var that = this;' yes What? What does it mean in JavaScript?

So Crockie's explanation is really wrong.... But his code is right, so he's still a good guy. )

#2 building

I think of this: Editing: in fact, someone is linked to the same solution. h!

var Car = function() {
}

Car.prototype = (function() {
    var hotWire = function() {
        // Private code *with* access to public properties through 'this'
        alert( this.drive() ); // Alerts 'Vroom!'
    }

    return {
        steal: function() {
            hotWire.call( this ); // Call a private method
        },
        drive: function() {
            return 'Vroom!';
        }
    };
})();

var getAwayVechile = new Car();

hotWire(); // Not allowed
getAwayVechile.hotWire(); // Not allowed
getAwayVechile.steal(); // Alerts 'Vroom!'

#3 building

If you want to use public functions to access all the public and private functions of private functions, lay out the code for such objects:

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

#4 building

Usually, I temporarily add a private Object to the Object. This method must have privacy fully turned on in the Power Builder. If the method is invoked from the prototype, it will be able to override the prototype method.

  • Make public methods in "power constructor" accessible: (ctx is the object context)

    ctx.test = GD.Fabric.open('test', GD.Test.prototype, ctx, _); // is a private object
  • Now I have this openPrivacy:

    GD.Fabric.openPrivacy = function(func, clss, ctx, _) { return function() { ctx._ = _; var res = clss[func].apply(ctx, arguments); ctx._ = null; return res; }; };

#5 building

You must enclose the actual constructor, where you can define private methods. To change instance data through these private methods, you must use them with the function, or "this" them by calling the function with. apply (this):

var Restaurant = (function(){
    var private_buy_food = function(that){
        that.data.soldFood = true;
    }
    var private_take_a_shit = function(){
        this.data.isdirty = true;   
    }
    // New Closure
    function restaurant()
    {
        this.data = {
            isdirty : false,
            soldFood: false,
        };
    }

    restaurant.prototype.buy_food = function()
    {
       private_buy_food(this);
    }
    restaurant.prototype.use_restroom = function()
    {
       private_take_a_shit.call(this);
    }
    return restaurant;
})()

// TEST:

var McDonalds = new Restaurant();
McDonalds.buy_food();
McDonalds.use_restroom();
console.log(McDonalds);
console.log(McDonalds.__proto__);

Posted by Drayton on Fri, 24 Jan 2020 02:30:47 -0800