JSFiddle - React, Tailwind, and code Playground

by kyllle

JavaScript

function Beer(type) {
    this.type = type;
    console.log(type);
    var volume = 0.5;
 
    var lessBeer = function() {
        volume = volume - 0.5;
    };
 
    this.drink = function() {
        if (volume === 0) {
            console.log('no more beer :(');
            return;
        }
        lessBeer();
        console.log('mmm beer.');
    };
};
 
var stout = new Beer('stout');
console.log(stout.type); // prints 'stout'
stout.drink();  //prints mmm beer
stout.drink();  //prints no more beer :(
 
stout.volume = 10; //trying to top up doesn't work! 
stout.drink();  //prints no more beer :(
stout.lessBeer() //TypeError? must be private