JavaScript private methods

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

    Personally, I prefer the following pattern for creating classes in JavaScript :

    var myClass = (function() {
        // Private class properties go here
    
        var blueprint = function() {
            // Private instance properties go here
            ...
        };
    
        blueprint.prototype = { 
            // Public class properties go here
            ...
        };
    
        return  {
             // Public class properties go here
            create : function() { return new blueprint(); }
            ...
        };
    })();
    

    As you can see, it allows you to define both class properties and instance properties, each of which can be public and private.


    Demo

    var Restaurant = function() {
        var totalfoodcount = 0;        // Private class property
        var totalrestroomcount  = 0;   // Private class property
        
        var Restaurant = function(name){
            var foodcount = 0;         // Private instance property
            var restroomcount  = 0;    // Private instance property
            
            this.name = name
            
            this.incrementFoodCount = function() {
                foodcount++;
                totalfoodcount++;
                this.printStatus();
            };
            this.incrementRestroomCount = function() {
                restroomcount++;
                totalrestroomcount++;
                this.printStatus();
            };
            this.getRestroomCount = function() {
                return restroomcount;
            },
            this.getFoodCount = function() {
                return foodcount;
            }
        };
       
        Restaurant.prototype = {
            name : '',
            buy_food : function(){
               this.incrementFoodCount();
            },
            use_restroom : function(){
               this.incrementRestroomCount();
            },
            getTotalRestroomCount : function() {
                return totalrestroomcount;
            },
            getTotalFoodCount : function() {
                return totalfoodcount;
            },
            printStatus : function() {
               document.body.innerHTML
                   += '

    Buying food at '+this.name+'

    ' + '
      ' + '
    • Restroom count at ' + this.name + ' : '+ this.getRestroomCount() + '
    • ' + '
    • Food count at ' + this.name + ' : ' + this.getFoodCount() + '
    • ' + '
    • Total restroom count : '+ this.getTotalRestroomCount() + '
    • ' + '
    • Total food count : '+ this.getTotalFoodCount() + '
    • ' + '
    '; } }; return { // Singleton public properties create : function(name) { return new Restaurant(name); }, printStatus : function() { document.body.innerHTML += '
    ' + '

    Overview

    ' + '
      ' + '
    • Total restroom count : '+ Restaurant.prototype.getTotalRestroomCount() + '
    • ' + '
    • Total food count : '+ Restaurant.prototype.getTotalFoodCount() + '
    • ' + '
    ' + '
    '; } }; }(); var Wendys = Restaurant.create("Wendy's"); var McDonalds = Restaurant.create("McDonald's"); var KFC = Restaurant.create("KFC"); var BurgerKing = Restaurant.create("Burger King"); Restaurant.printStatus(); Wendys.buy_food(); Wendys.use_restroom(); KFC.use_restroom(); KFC.use_restroom(); Wendys.use_restroom(); McDonalds.buy_food(); BurgerKing.buy_food(); Restaurant.printStatus(); BurgerKing.buy_food(); Wendys.use_restroom(); McDonalds.buy_food(); KFC.buy_food(); Wendys.buy_food(); BurgerKing.buy_food(); McDonalds.buy_food(); Restaurant.printStatus();

    See also this Fiddle.

提交回复
热议问题