/
basic.js
executable file
·46 lines (38 loc) · 1.21 KB
/
basic.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
* Example showing the basic concept behind private variables
*
*/
var Person = (function() {
// create a lookup function for easy private retrieval
function pvt(obj) {
if (typeof obj._pkg == 'undefined')
obj._pkg = store.push({_self : obj})-1;
// ensure obj === the original obj and not just an === ._pkg value
return store[obj._pkg]._self === obj ? store[obj._pkg] : null;
}
// create a closure variable to store privates in
var store = []; // private store for instances;
var chromosomes = 48; // private static variable
// create functions in the same prototypical/classical manner
var Self = function(isDancing) {
pvt(this).dancing = isDancing;
};
Self.prototype = {
// access the private variable in a public method
dance: function(){
return pvt(this).dancing;
},
// access the the private static variable in a public method
getChromosomeCount: function() {
return chromosomes;
}
};
return Self;
})();
// Check to see if private variables are in fact private
var p = new Person(true);
p.dance(); // => true
p.getChromosomeCount; // => 48
Person.getChromosomeCount; // => 48
p.dancing; // undefined
p.chromosomes; // undefined