1: function Person(first,last,age) { 2: this.firstname = first; 3: this.lastname = last; 4: this.age = age; 5:
var bankBalance = 7500; // private property which could not be called outside
6: } 7: // create your Person 8: var john = new Person("Guangyuan","Piao",29); 9: // try to print his bankBalance 10: console.log(john.bankBalance); // will print undefined
2.Create and call private function
1: function Person(first,last,age) {
2: this.firstname = first;
3: this.lastname = last;
4: this.age = age;
5: var bankBalance = 7500;
6: var returnBalance = function() {
7: return bankBalance;
8: };
9: // create the new function here
10: this.askTeller = function () {
11: return returnBalance;
12: }
13: }
14: var john = new Person('John','Smith',30);
15: console.log(john.returnBalance);
16: var myBalanceMethod = john.askTeller(); // return function
17: var myBalance = myBalanceMethod(); // call function
18: console.log(myBalance);
No comments:
Post a Comment