現下,javascript大行其道,對於網站開發人員來說,javascript是必需掌據的一門語言,但隨着jquery等框架的流行和使用,許多人對於原生javascript缺乏深入的理解,習慣了函數式的編輯風格,對於閉包、原型總是說不清道不明.對於js面向對象蹩腳的用着,而要了解js面向對象,就必需先了解js中什么是公有方法、特權方法、靜態方法
方法/步驟
1.公有屬性和公有方法
1
2
3
4
5
6
7
8
9
|
function User(name,age){
this .name = name; //公有屬性
this .age = age;
}
User.prototype.getName = function (){ //公有方法
return this .name;
}
var user = new User( 'fire子海' ,26);
console.log(user.getName()); //output:fire子海
|
2.私有屬性和方法
1
2
3
4
5
6
7
8
9
|
function User(name,age){
var name = name; //私有屬性
var age = age;
function alertAge(){ //私有方法
alert(age);
}
alertAge(age); //彈出26
}
var user = new User( 'fire子海' ,26);
|
3.靜態屬性和方法
在php中,無需實例化就可以調用的方法就叫靜態方法,js也一樣,無需實例化,即用new操作符實化對象,就可調用對象的方法和屬性。
1
2
3
4
5
6
7
8
|
function User(){}
User.age = 26; //靜態屬性
User.myname = 'fire子海' ;
User.getName = function (){ //靜態方法
return this .myname; //如果這里使用this.name,返回的將是User,所有改用了myname,
}
console.log(User.getName()); //output:fire子海
|
4.特權方法
1
2
3
4
5
6
7
8
9
|
function
User(name,age){
var
name = name;
//私有屬性
var
age = age;
this
.getName =
function
(){
//特權方法
return
name;
//私有屬性和方法不能使用this調用
}
}
var
user =
new
User(
'fire子海'
,26);
console.log(user.getName());
//output:fire子海
|
5.靜態類
對於靜態方法和靜態屬性,我們無需像第三步中那樣去創建,如果網友看過我那篇“js如何制作圖片輪播”,就知道可以使用字面量的方式來創建。
1
2
3
4
5
6
7
8
9
10
11
|
var
user = {
init:
function
(name,age){
this
.name = name;
this
.age = age;
},
getName:
function
(){
return
this
.name;
}
}
user.init(
'fire子海'
,26);
console.log(user.getName());
//output:fire子海
|
6.公有方法的調用規則
調用公有方法,我們必需先實例化對象
公有方法中通過不this調用公有屬性和特權方法,不能使用this調用靜態方法和屬性,必需裁通過對象本身調用,即對象名。公有方法也不能調用私有方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
function
User(){
this
.myname =
'fire子海'
;
//公有屬性
this
.age = 26;
this
.
do
=
function
(){
//特權方法
return
this
.myname+
'學習js'
;
}
}
User.eat =
function
(food){
return
'晚餐只有'
+food;
}
User.prototype.alertAge =
function
(){
alert(
this
.age);
}
User.prototype.alertDo =
function
(){
alert(
this
.
do
());
//調用特權方法
}
User.prototype.alertEat =
function
(food){
alert(User.eat(food));
//只能通過對象本身調用靜態方法
//alert(this.ear(food))這樣調用將出錯:this.eat is not a function
}
var
user =
new
User();
user.alertAge();
//alert:26
user.alertDo();
//alert:fire子海學習js
user.alertEat(
'方便面'
)
//alert:晚餐只有方便面
|
7.靜態方法的調用規則
使用靜態方法時,無需實例化對象,便可以調用,對象實例不能調用對象的靜態方法,只能調用實例自身的靜態屬性和方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
function
User(){}
User.age = 26;
//靜態屬性
User.myname =
'fire子海'
;
User.getName =
function
(){
//靜態方法
return
this
.myname;
}
var
user =
new
User();
console.log(user.getName);
//TypeError: user.getName is not a function
user.supper =
'方便面'
;
user.eat =
function
(){
return
'晚餐只有'
+
this
.supper;
}
user.eat();
//晚餐只有方便面
|
靜態方法無法調用公有屬性、公有方法、私有方法、私有屬性、特權方法和原型屬性
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
function
User(){
this
.myname =
'fire子海'
;
//公有屬性
this
.age = 26;
this
.
do
=
function
(){
//特權方法
return
this
.myname+
'學習js'
;
}
}
User.prototype.alertAge =
function
(){
//公共方法,也叫原型方法
alert(
this
.age);
}
User.prototype.sex =
'男'
;
//原型屬性
User.getName=
function
(){
//靜態方法
return
this
.myname;
}
User.getAge =
function
(){
this
.alertAge();
}
User.getDo =
function
(){
return
this
.
do
();
}
//console.log(User.getName())//undefined
//console.log(User.getDo());//TypeError: this.do is not a function
//console.log(User.getAge())//TypeError: this.alertAge is not a function
|
8.特權方法的調用規則
特權方法通過this調用公有方法、公有屬性,通過對象本身調用靜態方法和屬性,在方法體內直接調用私有屬性和私有方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
function
User(girlfriend){
var
girlfriend = girlfriend;
function
getGirlFriend(){
return
'我女朋友'
+girlfriend+
'是美女!'
;
}
this
.myname =
'fire子海'
;
//公有屬性
this
.age = 26;
this
.
do
=
function
(){
//特權方法
return
this
.myname+
'學習js'
;
}
this
.alertAge =
function
(){
this
.changeAge();
//特權方法調用公有方法
alert(
this
.age);
}
this
.alertGirlFriend =
function
(){
alert(getGirlFriend());
//調用私有方法
}
}
User.prototype.changeAge =
function
(){
this
.age = 29;
}
var
user =
new
User(
'某某'
);
user.alertAge();
//alert:29
user.alertGirlFriend();
//alert:我的女朋友某某是美女!
|
9.私有方法
對象的私有方法和屬性,外部是不可以訪問的,在方法的內部不是能this調用對象的公有方法、公有屬性、特權方法的
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
|
function
User(girlfriend){
var
girlfriend = girlfriend;
this
.myname =
'fire子海'
;
//公有屬性
this
.age = 26;
function
getGirlFriend(){
//this.myname ;//此時的this指向的window對象,並非User對象,
// this.myname = 'fire子海',此時的this指向的是getGirFriend對象了。
//如果通過this調用了getGirFriend中不存在的方法呀屬性,this便會指向window 對象,只有this調用了getGirlFriend存在的方法和屬性,this才會指定getGirlFriend;
alert(User.eat(
'泡面'
));
//alert:晚餐只有方便面
}
this
.
do
=
function
(){
//特權方法
return
this
.myname+
'學習js'
;
}
this
.alertAge =
function
(){
this
.changeAge();
//特權方法調用公有方法
alert(
this
.age);
}
this
.alertGirlFriend =
function
(){
getGirlFriend();
//調用私有方法
}
}
User.eat =
function
(supper){
return
'晚餐只有'
+supper;
}
var
user =
new
User(
'某某'
);
user.alertGirlFriend();
總結:給對象動態添加的屬性或方法是靜態屬性或靜態方法;
var obj={};
obj.name="張三";//靜態屬性
obj.run=function(){
console.log(obj.name);//靜態的方法
}
obj.run();//進行調用
|