Return to Snippet

Revision: 63229
at April 17, 2013 21:30 by Tim_McClure


Initial Code
function Person(first,last,age) {
   this.firstname = first;
   this.lastname = last;
   this.age = age;
   var bankBalance = 7500;
}

// create your Person 
var Tim = new Person("Tim","McClure",23);

// try to print his bankBalance
console.log(Tim.bankBalance);

Initial URL


Initial Description
Just as functions can have local variables which can only be accessed from within that function, objects can have private variables. Private variables are pieces of information you do not want to publicly share, and they can only be directly accessed from within the class.

The Person class has been modified to have a private variable called bankBalance. Notice that it looks just like a normal variable, but it is defined inside the constructor for Person without using this, but instead using var. This makes bankBalance a private variable.

Initial Title
Public vs. Private Variables

Initial Tags


Initial Language
JavaScript