JavaScript private methods

前端 未结 30 1879
-上瘾入骨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 09:07

    In general I added the private Object _ temporarily to the object. You have to open the privacy exlipcitly in the "Power-constructor" for the method. If you call the method from the prototype, you will be able to overwrite the prototype-method

    • Make a public method accessible in the "Power-constructor": (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;
          };
      };
      

提交回复
热议问题