Javascript 面向?qū)ο螅ǘ┓庋b代碼
更新時(shí)間:2012年05月23日 17:27:14 作者:
Javascript 面向?qū)ο螅ǘ┓庋b代碼,需要的朋友可以參考下
寫個(gè)小例子:
第一步:做一個(gè)“手機(jī)的類"
var MobilePhone = (function(){
…………
})()
第二步:考慮這個(gè)類,里需要那些類的私有屬性,這里我想定義的是實(shí)例出來(lái)手機(jī)的數(shù)量
var MobilePhone = (function(){
//私有屬性
var count = 0; //代表手機(jī)的數(shù)量
})()
第三步:創(chuàng)建一個(gè)構(gòu)造函數(shù),即實(shí)例時(shí)候,對(duì)產(chǎn)生的新象的一個(gè)初始化,例如屬性,方法的初始化;在這個(gè)例子中,每一個(gè)手機(jī)都會(huì)有顏色,大小,價(jià)格屬性.這里的構(gòu)造函數(shù)也是一個(gè)閉包,所以可以訪問(wèn)count,并且count的值會(huì)長(zhǎng)期保存在內(nèi)存中(只要有引用存在)
var MobilePhone = (function(){
//私有屬性
var count = 0; //代表手機(jī)的數(shù)量
//構(gòu)造函數(shù)
var creatphone = function(color,size,price){
count++;
this._color = color; //手機(jī)的顏色
this._size = size; //手機(jī)的大小
this._price = price; //手機(jī)的價(jià)格
this.index = count; //手機(jī)索引,是第幾臺(tái)創(chuàng)建的手機(jī)手象
}
})()
第四步:共有方法:
即所有實(shí)例出來(lái)的手機(jī)對(duì)象,都能使用的方法,這里手機(jī)應(yīng)該可以改變價(jià)格,顏色,大小,也可以顯示大小,顏色,價(jià)格。
這里的共有方法應(yīng)該放在“原型對(duì)象”中:
1.所有通過(guò)該構(gòu)造函數(shù)實(shí)例的對(duì)象,也就是造出的手機(jī),都能使用“原型對(duì)象”中的方法。
2.如果放在構(gòu)造函數(shù)中,那么每一次實(shí)例一個(gè)手機(jī)對(duì)象出來(lái),都會(huì)產(chǎn)生一堆重復(fù)的方法,占用內(nèi)存。
3."constructor":creatphone解釋:
因?yàn)閏reatphone.prototype ={……}相當(dāng)對(duì)把之前的原型對(duì)象的引用,給復(fù)蓋掉了。而為了讓原型對(duì)象和該構(gòu)造函數(shù)關(guān)聯(lián)起來(lái) 設(shè)置了"constructor":creatphone,這個(gè)屬性.
var MobilePhone = (function(){
//私有屬性
var count = 0;//代表手機(jī)的數(shù)量
//構(gòu)造函數(shù)
var creatphone = function(color,size,price){
count++;
this._color = color; //手機(jī)的顏色
this._size = size; //手機(jī)的大小
this._price = price; //手機(jī)的價(jià)格
this.index = count; //手機(jī)索引,是第幾臺(tái)創(chuàng)建的手機(jī)手象
}
//公有方法,存放在原型對(duì)象中
creatphone.prototype = {
"constructor":creatphone,
//獲取手機(jī)顏色
"getColor" : function(){
return this._color;
},
//設(shè)置手機(jī)顏色
"setColor" : function(color){
this._color = color;
},
//獲取手機(jī)大小
"getSize" : function(){
return "width:"+this._size.width + " height:" + this._size.height;
},
//設(shè)置手機(jī)大小
"setSize" : function(size){
this._size.width = size.width;
this._size.height = size.height;
},
//獲取手機(jī)價(jià)格
"getPrice" : function(){
return this._price;
},
//設(shè)置手機(jī)價(jià)格
"setPrice" : function(price){
this._price = price
}
}
})()
第五步:特權(quán)方法,即需要有一個(gè)方法,能夠去訪問(wèn)類的私有變量。就是實(shí)例出來(lái)多少臺(tái)手機(jī)對(duì)象
var MobilePhone = (function(){
//私有屬性
var count = 0;//代表手機(jī)的數(shù)量
var index = 0;//代表手機(jī)的索引
//構(gòu)造函數(shù)
var creatphone = function(color,size,price){
count++;
this._color = color; //手機(jī)的顏色
this._size = size; //手機(jī)的大小
this._price = price; //手機(jī)的價(jià)格
this.index = count; //手機(jī)索引,是第幾臺(tái)創(chuàng)建的手機(jī)手象
} //公有方法,存放在原型對(duì)象中
creatphone.prototype = {
"constructor":creatphone,
"getColor" : function(){
return this._color;
},
"setColor" : function(color){
this._color = color;
},
"getSize" : function(){
return "width:"+this._size.width + " height:" + this._size.height;
},
"setSize" : function(size){
this._size.width = size.width;
this._size.height = size.height;
},
"getPrice" : function(){
return this._price;
},
"setPrice" : function(price){
this._price = price
}
}
//特權(quán)方法
creatphone.get_count_index = function(){
return count
}
return creatphone;
})()
用上面封裝的一個(gè)手機(jī)類 測(cè)試
var anycall = new MobilePhone(); //實(shí)例一個(gè)三星手機(jī)對(duì)象
var HTC = new MobilePhone(); //實(shí)例一個(gè)HTC手機(jī)對(duì)象
var Iphone4s = new MobilePhone(); //實(shí)例一個(gè)蘋果4S手機(jī)對(duì)象
console.log("三星是第:"+anycall.index+"臺(tái)"); //FF的控制臺(tái)輸出三星手機(jī)對(duì)象是第幾臺(tái)創(chuàng)建的,即索引;
console.log("HTC是第:"+HTC.index+"臺(tái)"); //FF的控制臺(tái)輸出HTC手機(jī)對(duì)象是第幾臺(tái)創(chuàng)建的,即索引;
console.log("Iphone4s是第:"+Iphone4s.index+"臺(tái)"); //FF的控制臺(tái)輸出個(gè)蘋果4S手機(jī)對(duì)象是第幾臺(tái)創(chuàng)建的,即索引;
console.log("總共造出了"+MobilePhone.get_count_index()+"手機(jī)"); //FF的控制臺(tái)輸出總共創(chuàng)建了幾臺(tái)手機(jī);
console.log(anycall.constructor === MobilePhone); //實(shí)例出來(lái)的對(duì)象,的原形象中的constructor,是否還指向MobilePhone
第一步:做一個(gè)“手機(jī)的類"
復(fù)制代碼 代碼如下:
var MobilePhone = (function(){
…………
})()
第二步:考慮這個(gè)類,里需要那些類的私有屬性,這里我想定義的是實(shí)例出來(lái)手機(jī)的數(shù)量
復(fù)制代碼 代碼如下:
var MobilePhone = (function(){
//私有屬性
var count = 0; //代表手機(jī)的數(shù)量
})()
第三步:創(chuàng)建一個(gè)構(gòu)造函數(shù),即實(shí)例時(shí)候,對(duì)產(chǎn)生的新象的一個(gè)初始化,例如屬性,方法的初始化;在這個(gè)例子中,每一個(gè)手機(jī)都會(huì)有顏色,大小,價(jià)格屬性.這里的構(gòu)造函數(shù)也是一個(gè)閉包,所以可以訪問(wèn)count,并且count的值會(huì)長(zhǎng)期保存在內(nèi)存中(只要有引用存在)
復(fù)制代碼 代碼如下:
var MobilePhone = (function(){
//私有屬性
var count = 0; //代表手機(jī)的數(shù)量
//構(gòu)造函數(shù)
var creatphone = function(color,size,price){
count++;
this._color = color; //手機(jī)的顏色
this._size = size; //手機(jī)的大小
this._price = price; //手機(jī)的價(jià)格
this.index = count; //手機(jī)索引,是第幾臺(tái)創(chuàng)建的手機(jī)手象
}
})()
第四步:共有方法:
即所有實(shí)例出來(lái)的手機(jī)對(duì)象,都能使用的方法,這里手機(jī)應(yīng)該可以改變價(jià)格,顏色,大小,也可以顯示大小,顏色,價(jià)格。
這里的共有方法應(yīng)該放在“原型對(duì)象”中:
1.所有通過(guò)該構(gòu)造函數(shù)實(shí)例的對(duì)象,也就是造出的手機(jī),都能使用“原型對(duì)象”中的方法。
2.如果放在構(gòu)造函數(shù)中,那么每一次實(shí)例一個(gè)手機(jī)對(duì)象出來(lái),都會(huì)產(chǎn)生一堆重復(fù)的方法,占用內(nèi)存。
3."constructor":creatphone解釋:
因?yàn)閏reatphone.prototype ={……}相當(dāng)對(duì)把之前的原型對(duì)象的引用,給復(fù)蓋掉了。而為了讓原型對(duì)象和該構(gòu)造函數(shù)關(guān)聯(lián)起來(lái) 設(shè)置了"constructor":creatphone,這個(gè)屬性.
復(fù)制代碼 代碼如下:
var MobilePhone = (function(){
//私有屬性
var count = 0;//代表手機(jī)的數(shù)量
//構(gòu)造函數(shù)
var creatphone = function(color,size,price){
count++;
this._color = color; //手機(jī)的顏色
this._size = size; //手機(jī)的大小
this._price = price; //手機(jī)的價(jià)格
this.index = count; //手機(jī)索引,是第幾臺(tái)創(chuàng)建的手機(jī)手象
}
//公有方法,存放在原型對(duì)象中
creatphone.prototype = {
"constructor":creatphone,
//獲取手機(jī)顏色
"getColor" : function(){
return this._color;
},
//設(shè)置手機(jī)顏色
"setColor" : function(color){
this._color = color;
},
//獲取手機(jī)大小
"getSize" : function(){
return "width:"+this._size.width + " height:" + this._size.height;
},
//設(shè)置手機(jī)大小
"setSize" : function(size){
this._size.width = size.width;
this._size.height = size.height;
},
//獲取手機(jī)價(jià)格
"getPrice" : function(){
return this._price;
},
//設(shè)置手機(jī)價(jià)格
"setPrice" : function(price){
this._price = price
}
}
})()
第五步:特權(quán)方法,即需要有一個(gè)方法,能夠去訪問(wèn)類的私有變量。就是實(shí)例出來(lái)多少臺(tái)手機(jī)對(duì)象
復(fù)制代碼 代碼如下:
var MobilePhone = (function(){
//私有屬性
var count = 0;//代表手機(jī)的數(shù)量
var index = 0;//代表手機(jī)的索引
//構(gòu)造函數(shù)
var creatphone = function(color,size,price){
count++;
this._color = color; //手機(jī)的顏色
this._size = size; //手機(jī)的大小
this._price = price; //手機(jī)的價(jià)格
this.index = count; //手機(jī)索引,是第幾臺(tái)創(chuàng)建的手機(jī)手象
} //公有方法,存放在原型對(duì)象中
creatphone.prototype = {
"constructor":creatphone,
"getColor" : function(){
return this._color;
},
"setColor" : function(color){
this._color = color;
},
"getSize" : function(){
return "width:"+this._size.width + " height:" + this._size.height;
},
"setSize" : function(size){
this._size.width = size.width;
this._size.height = size.height;
},
"getPrice" : function(){
return this._price;
},
"setPrice" : function(price){
this._price = price
}
}
//特權(quán)方法
creatphone.get_count_index = function(){
return count
}
return creatphone;
})()
用上面封裝的一個(gè)手機(jī)類 測(cè)試
復(fù)制代碼 代碼如下:
var anycall = new MobilePhone(); //實(shí)例一個(gè)三星手機(jī)對(duì)象
var HTC = new MobilePhone(); //實(shí)例一個(gè)HTC手機(jī)對(duì)象
var Iphone4s = new MobilePhone(); //實(shí)例一個(gè)蘋果4S手機(jī)對(duì)象
console.log("三星是第:"+anycall.index+"臺(tái)"); //FF的控制臺(tái)輸出三星手機(jī)對(duì)象是第幾臺(tái)創(chuàng)建的,即索引;
console.log("HTC是第:"+HTC.index+"臺(tái)"); //FF的控制臺(tái)輸出HTC手機(jī)對(duì)象是第幾臺(tái)創(chuàng)建的,即索引;
console.log("Iphone4s是第:"+Iphone4s.index+"臺(tái)"); //FF的控制臺(tái)輸出個(gè)蘋果4S手機(jī)對(duì)象是第幾臺(tái)創(chuàng)建的,即索引;
console.log("總共造出了"+MobilePhone.get_count_index()+"手機(jī)"); //FF的控制臺(tái)輸出總共創(chuàng)建了幾臺(tái)手機(jī);
console.log(anycall.constructor === MobilePhone); //實(shí)例出來(lái)的對(duì)象,的原形象中的constructor,是否還指向MobilePhone
結(jié)果如下,全完正確:
您可能感興趣的文章:
- javascript面向?qū)ο蟀b類Class封裝類庫(kù)剖析
- Javascript 面向?qū)ο缶幊蹋ㄒ唬?封裝
- JS面向?qū)ο缶幊虒?shí)現(xiàn)的拖拽功能案例詳解
- JS基于面向?qū)ο髮?shí)現(xiàn)的拖拽功能示例
- jquery方法+js一般方法+js面向?qū)ο蠓椒▽?shí)現(xiàn)拖拽效果
- JS實(shí)現(xiàn)鼠標(biāo)按下拖拽效果
- JavaScript鼠標(biāo)拖拽事件詳解
- 純 JS 實(shí)現(xiàn)放大縮小拖拽功能(完整代碼)
- JavaScript實(shí)現(xiàn)圖片的放大縮小及拖拽功能示例
- 詳解JavaScript面向?qū)ο髮?shí)戰(zhàn)之封裝拖拽對(duì)象
相關(guān)文章
手把手教你自己寫一個(gè)js表單驗(yàn)證框架的方法
其實(shí)我自己也就能簡(jiǎn)單用用js而已,但是呢,相對(duì)很多初學(xué)者來(lái)說(shuō)多懂了點(diǎn)Know How所以斗膽孟浪一下,將一些所得記錄下來(lái),以供更多的初學(xué)者能夠知道一個(gè)東西的實(shí)現(xiàn)過(guò)程,省去在源碼里摸索的過(guò)程。2010-09-09

不錯(cuò)的JavaScript面向?qū)ο蟮暮?jiǎn)單入門介紹
JavaScript是一門OOP,而有些人說(shuō),JavaScript是基于對(duì)象的。
2008-07-07