JavaScript private methods

前端 未结 30 1485
-上瘾入骨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:05

    I know it's a bit too late but how about this?

    var obj = function(){
        var pr = "private";
        var prt = Object.getPrototypeOf(this);
        if(!prt.hasOwnProperty("showPrivate")){
            prt.showPrivate = function(){
                console.log(pr);
            }
        }    
    }
    
    var i = new obj();
    i.showPrivate();
    console.log(i.hasOwnProperty("pr"));
    
    0 讨论(0)
  • 2020-11-22 09:07

    You can simulate private methods like this:

    function Restaurant() {
    }
    
    Restaurant.prototype = (function() {
        var private_stuff = function() {
            // Private code here
        };
    
        return {
    
            constructor:Restaurant,
    
            use_restroom:function() {
                private_stuff();
            }
    
        };
    })();
    
    var r = new Restaurant();
    
    // This will work:
    r.use_restroom();
    
    // This will cause an error:
    r.private_stuff();
    

    More information on this technique here: http://webreflection.blogspot.com/2008/04/natural-javascript-private-methods.html

    0 讨论(0)
  • 2020-11-22 09:07

    The apotheosis of the Module Pattern: The Revealing Module Pattern

    A neat little extension to a very robust pattern.

    0 讨论(0)
  • 2020-11-22 09:07

    Private functions cannot access the public variables using module pattern

    0 讨论(0)
  • 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;
          };
      };
      
    0 讨论(0)
  • 2020-11-22 09:08

    Since everybody was posting here his own code, I'm gonna do that too...

    I like Crockford because he introduced real object oriented patterns in Javascript. But he also came up with a new misunderstanding, the "that" one.

    So why is he using "that = this"? It has nothing to do with private functions at all. It has to do with inner functions!

    Because according to Crockford this is buggy code:

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

    So he suggested doing this:

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

    So as I said, I'm quite sure that Crockford was wrong his explanation about that and this (but his code is certainly correct). Or was he just fooling the Javascript world, to know who is copying his code? I dunno...I'm no browser geek ;D

    EDIT

    Ah, that's what is all about: What does 'var that = this;' mean in JavaScript?

    So Crockie was really wrong with his explanation....but right with his code, so he's still a great guy. :))

    0 讨论(0)
提交回复
热议问题