Categories

Private members in JavaScript

Posted on: December 24, 2013 by Dimitar Ivanov

Private members in Constructors

To implement private members you need to use closure. Constructor functions create a closure and their variables are not visible outside the constructor.

  • Private members - you can add private members in function' constructor using var. All constructor' attributes are also private.
  • Private methods - you can add private methods as inner function in the constructor. This methods are accessible only in the constructor.
  • Privileged methods - you can add privileged methods using this variable in the constructor. All private members and methods are visible inside the privilege methods. Also all privileged methods are visible in the prototype (public methods).
<script type="text/javascript">
function Dog(breed, name, color) {
    //private members
    var breed = breed,
        name = name,
        color = color;

    //private method
    function getColor() {
        return color;
    }
    //private method
    var getName = function () {
        return name;
    }

    //privileged method
    this.getMessage = function () {
        return getColor() + ' dog, ' + breed + 
            ' breed, named ' + getName() + ' is missing';
    };
}

var doggy = new Dog('Jack Russell', 'Charlie', 'white');
console.log(doggy.name); //undefined
console.log(doggy.color); //undefined
console.log(doggy.breed); //undefined
console.log(doggy.getName()); //Uncaught TypeError: Object # has no method 'getName'
console.log(doggy.getColor()); //Uncaught TypeError: Object # has no method 'getColor' 
console.log(doggy.getMessage()); //white dog, Jack Russell breed, named Charlie is missing 
</script>

Private members and Object Literals

If our objects are created with object literals it is still possible to have private members. In this case you need to use Module Pattern.

<script type="text/javascript">
var doggy = (function () {
    //private members
    var breed = 'Beagle',
        name = 'Benny',
        color = 'brown';

    //private methods
    function getBreed() {
        return breed;
    }
    
    //public interface
    return {
        getName: function () {
            return name;
        },
        getColor: function () {
            return  color;
        },
        getMessage: function () {
            return this.getColor() + ' dog, ' + 
                getBreed() + ' breed, named ' + 
                this.getName() + ' is missing';
        },
    };
})();

console.log(doggy.name); //undefined
console.log(doggy.color); //undefined
console.log(doggy.breed); //undefined
console.log(doggy.getName()); //Benny
console.log(doggy.getColor()); //brown
console.log(doggy.getBreed()); //Uncaught TypeError: Object #Object has no method 'getBreed' 
console.log(doggy.getMessage()); //brown dog, Beagle breed, named Benny is missing
</script>

Private members and Prototypes

To avoid re-creation of private members, when every time the constructor is invoked to create a new object, and save memory you can move common members and methods to the prototype property of the constructor.

<script type="text/javascript">
function Dog(name, color) {
    //private members
    var name = name,
        color = color;

    //public method
    this.getColor = function () {
        return color;
    }
    //public method
    this.getName = function () {
        return name;
    }
}
Dog.prototype = (function () {
    //private member
    var breed = 'French Bulldog',
    
    //private method
    getBreed = function () {
        return breed;
    };
    
    //public interface
    return {
        getBreed: function () {
            return breed;
        },
        getMessage: function () {
            return this.getColor() + ' dog, ' + 
                getBreed() + ' breed, named ' + 
                this.getName() + ' is missing';
        }
    }
})();

var doggy = new Dog('Juliet', 'black');
console.log(doggy.name); //undefined
console.log(doggy.color); //undefined
console.log(doggy.breed); //undefined
console.log(doggy.getName()); //Juliet
console.log(doggy.getColor()); //black
console.log(doggy.getBreed()); //French Bulldog
console.log(doggy.getMessage()); //black dog, French Bulldog breed, named Juliet is missing
</script>
See also
Social sharing

If you have questions about private members in javascript, please leave a comment below. And do not be shy to share this article. Thanks for reading!


1 Comment

sitraka
Thanks, it helps a lot

Comments are closed