与java等基于类的面向对象语言的private、protected、public等关键字的用途类似,基于对象的JavaScript语言,在对象构造上也存在类似的成员可见性问题。
JavaScript对象构造的可见性定义可以分为以下几种:
私有属性(private properties)
通过var关键字定义对象构造中变量的作用域,该变量只能在对象构造方法的作用域内被访问。如:1
2
3
4
5
6function VariableTest()
{
var myVariable;//private
}
var vt = new VariableTest();
vt.myVariable;//这里会出现undefined异常
私有方法(private methods)
与私有属性类似,只能在对象构造方法作用域内被访问。如:1
2
3
4
5
6
7
8
9
10
11
12
13function MethodTest() {
var myMethod = function() //private
{
alert("private method");
}
this.invoke = function() {
//能够访问到myMethod
myMehtod();
}
}
var mt = new MethodTest();
mt.myMethod(); //错误。使用trycatch的话,可捕获“对象不支持此属性或方法”异常
mt.invoke();
公共属性(public properties)
有两种定义公共属性的途径:
(1)通过this关键字来定义。如:1
2
3
4
5function PrivilegedVariable() {
this.variable = "privileged variable";
}
var pv = new PrivilegedVariable();
pv.variable; //返回 "privileged variable"
(2)通过构造方法的原型来定义。如:1
2
3
4function PublicVariable() {}
PublicVariable.prototype.variable = "public variable";
var pv = new PublicVariable();
pv.variable; //返回"public variable"
公共方法(public methods)
同理,有两种定义公共方法的途径。
- 通过this关键字来定义。
- 通过构造方法的原型来定义。
这里省略。。。。。。。。。。。
静态属性(static properties)
直接为对象构造方法添加的属性,不能被对象实例访问,只能供构造方法自身使用。如:1
2
3
4
5
6function StaticVariable() {}
StaticVariable.variable = "static variable";
var sv = new StaticVariable();
sv.variable; //返回"undefined"
StaticVariable.prototype.variable; //返回"undefined"
StaticVariable.variable; //返回"static variable"
静态方法(static methods)
直接为对象构造方法添加的方法,不能被对象实例访问,只能供构造方法自身使用。
代码省略。。。。。。。。
Example: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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109function Person(n,race){
this.constructor.population++;
// ************************************************************************
// PRIVATE VARIABLES AND FUNCTIONS
// ONLY PRIVELEGED METHODS MAY VIEW/EDIT/INVOKE
// ***********************************************************************
var alive=true, age=1;
var maxAge=70+Math.round(Math.random()*15)+Math.round(Math.random()*15);
function makeOlder(){ return alive = (++age <= maxAge) }
var myName=n?n:"John Doe";
var weight=1;
// ************************************************************************
// PRIVILEGED METHODS
// MAY BE INVOKED PUBLICLY AND MAY ACCESS PRIVATE ITEMS
// MAY NOT BE CHANGED; MAY BE REPLACED WITH PUBLIC FLAVORS
// ************************************************************************
this.toString=this.getName=function(){ return myName }
this.eat=function(){
if (makeOlder()){
this.dirtFactor++;
return weight*=3;
} else alert(myName+" can't eat, he's dead!");
}
this.exercise=function(){
if (makeOlder()){
this.dirtFactor++;
return weight/=2;
} else alert(myName+" can't exercise, he's dead!");
}
this.weigh=function(){ return weight }
this.getRace=function(){ return race }
this.getAge=function(){ return age }
this.muchTimePasses=function(){ age+=50; this.dirtFactor=10; }
// ************************************************************************
// PUBLIC PROPERTIES -- ANYONE MAY READ/WRITE
// ************************************************************************
this.clothing="nothing/naked";
this.dirtFactor=0;
}
// ************************************************************************
// PUBLIC METHODS -- ANYONE MAY READ/WRITE
// ************************************************************************
Person.prototype.beCool = function(){ this.clothing="khakis and black shirt" }
Person.prototype.shower = function(){ this.dirtFactor=2 }
Person.prototype.showLegs = function(){ alert(this+" has "+this.legs+" legs") }
Person.prototype.amputate = function(){ this.legs-- }
// ************************************************************************
// PROTOTYOPE PROERTIES -- ANYONE MAY READ/WRITE (but may be overridden)
// ************************************************************************
Person.prototype.legs=2;
// ************************************************************************
// STATIC PROPERTIES -- ANYONE MAY READ/WRITE
// ************************************************************************
Person.population = 0;
// Here is the code that uses the Person class
function RunGavinsLife(){
var gk=new Person("Gavin","caucasian"); //New instance of the Person object created.
var lk=new Person("Lisa","caucasian"); //New instance of the Person object created.
alert("There are now "+Person.population+" people");
gk.showLegs(); lk.showLegs(); //Both share the common 'Person.prototype.legs' variable when looking at 'this.legs'
gk.race = "hispanic"; //Sets a public variable, but does not overwrite private 'race' variable.
alert(gk+"'s real race is "+gk.getRace()); //Returns 'caucasian' from private 'race' variable set at create time.
gk.eat(); gk.eat(); gk.eat(); //weight is 3...then 9...then 27
alert(gk+" weighs "+gk.weigh()+" pounds and has a dirt factor of "+gk.dirtFactor);
gk.exercise(); //weight is now 13.5
gk.beCool(); //clothing has been update to current fashionable levels
gk.clothing="Pimp Outfit"; //clothing is a public variable that can be updated to any funky value
gk.shower();
alert("Existing shower technology has gotten "+gk+" to a dirt factor of "+gk.dirtFactor);
gk.muchTimePasses(); //50 Years Pass
Person.prototype.shower=function(){ //Shower technology improves for everyone
this.dirtFactor=0;
}
gk.beCool=function(){ //Gavin alone gets new fashion ideas
this.clothing="tinfoil";
};
gk.beCool(); gk.shower();
alert("Fashionable "+gk+" at "
+gk.getAge()+" years old is now wearing "
+gk.clothing+" with dirt factor "
+gk.dirtFactor);
gk.amputate(); //Uses the prototype property and makes a public property
gk.showLegs(); lk.showLegs(); //Lisa still has the prototype property
gk.muchTimePasses(); //50 Years Pass...Gavin is now over 100 years old.
gk.eat(); //Complains about extreme age, death, and inability to eat.
}
From:JavaScript Public And Private Properties And Methods - MySQL, Oracle Abc Wiki