-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprivate-methods.js
More file actions
31 lines (29 loc) · 1.04 KB
/
private-methods.js
File metadata and controls
31 lines (29 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// private methods can be called by other methods in the same class,
// but javascript does not have a native way of doing this.
// It is possibe to emulate private methods with closures.
var counter = (function(){
var _privateCounter = 0;
function changeBy(val){
_privateCounter += val;
}
return{
increment : function(){
changeBy(1);
},
decrement : function(){
changeBy(-1);
},
value : function(){
return _privateCounter;
}
}
})();
// After execution/self invocation of the anonymous function it is going to return into variable counter.
// Counter variable will hold shared environment including private variable and private function changeBy.
// We can only be able to acsess returned functions (increment, decrement and value) like public methods through counter (counter.public-method-name)
console.log(counter.value());
counter.increment();
counter.increment();
console.log(counter.value());
counter.decrement();
console.log(counter.value());