js中prototype用法詳細(xì)介紹
prototype 是在 IE 4 及其以后版本引入的一個(gè)針對(duì)于某一類(lèi)的對(duì)象的方法,而且特殊的地方便在于:它是一個(gè)給類(lèi)的對(duì)象添加方法的方法!這一點(diǎn)可能聽(tīng)起來(lái)會(huì)有點(diǎn)亂,別急,下面我便通過(guò)實(shí)例對(duì)這一特殊的方法作已下講解:
首先,我們要先了解一下類(lèi)的概念,JavaScript 本身是一種面向?qū)ο蟮恼Z(yǔ)言,它所涉及的元素根據(jù)其屬性的不同都依附于某一個(gè)特定的類(lèi)。我們所常見(jiàn)的類(lèi)包括:數(shù)組變量(Array)、邏輯變量(Boolean)、日期變量(Date)、結(jié)構(gòu)變量(Function)、數(shù)值變量(Number)、對(duì)象變量(Object)、字符串變量(String) 等,而相關(guān)的類(lèi)的方法,也是程序員經(jīng)常用到的(在這里要區(qū)分一下類(lèi)的注意和屬性發(fā)方法),例如數(shù)組的push方法、日期的get系列方法、字符串的split方法等等,
但是在實(shí)際的編程過(guò)程中不知道有沒(méi)有感覺(jué)到現(xiàn)有方法的不足?prototype 方法應(yīng)運(yùn)而生!下面,將通過(guò)實(shí)例由淺入深講解 prototype 的具體使用方法:
1、最簡(jiǎn)單的例子,了解 prototype:
(1) Number.add(num):作用,數(shù)字相加
實(shí)現(xiàn)方法:Number.prototype.add = function(num){return(this+num);}
試驗(yàn):alert((3).add(15)) -> 顯示 18
(2) Boolean.rev(): 作用,布爾變量取反
實(shí)現(xiàn)方法:Boolean.prototype.rev = function(){return(!this);}
試驗(yàn):alert((true).rev()) -> 顯示 false
是不是很簡(jiǎn)單?這一節(jié)僅僅是告訴讀者又這么一種方法,這種方法是這樣運(yùn)用的。
2、已有方法的實(shí)現(xiàn)和增強(qiáng),初識(shí) prototype:
(1) Array.push(new_element)
作用:在數(shù)組末尾加入一個(gè)新的元素
實(shí)現(xiàn)方法:
Array.prototype.push = function(new_element){
this[this.length]=new_element;
return this.length;
}
讓我們進(jìn)一步來(lái)增強(qiáng)他,讓他可以一次增加多個(gè)元素!
實(shí)現(xiàn)方法:
Array.prototype.pushPro = function() {
var currentLength = this.length;
for (var i = 0; i < arguments.length; i++) {
this[currentLength + i] = arguments[i];
}
return this.length;
}
應(yīng)該不難看懂吧?以此類(lèi)推,你可以考慮一下如何通過(guò)增強(qiáng) Array.pop 來(lái)實(shí)現(xiàn)刪除任意位置,任意多個(gè)元素(具體代碼就不再細(xì)說(shuō)了)
(2) String.length
作用:這實(shí)際上是 String 類(lèi)的一個(gè)屬性,但是由于 JavaScript 將全角、半角均視為是一個(gè)字符,在一些實(shí)際運(yùn)用中可能會(huì)造成一定的問(wèn)題,現(xiàn)在我們通過(guò) prototype 來(lái)彌補(bǔ)這部不足。
實(shí)現(xiàn)方法:
String.prototype.cnLength = function(){
var arr=this.match(/[^\x00-\xff]/ig);
return this.length+(arr==null?0:arr.length);
}
試驗(yàn):alert("EaseWe空間Spaces".cnLength()) -> 顯示 16
這里用到了一些正則表達(dá)式的方法和全角字符的編碼原理,由于屬于另兩個(gè)比較大的類(lèi)別,本文不加說(shuō)明,請(qǐng)參考相關(guān)材料。
3、新功能的實(shí)現(xiàn),深入 prototype:在實(shí)際編程中所用到的肯定不只是已有方法的增強(qiáng),更多的實(shí)行的功能的要求,下面我就舉兩個(gè)用 prototype 解決實(shí)際問(wèn)題的例子:
(1) String.left()
問(wèn)題:用過(guò) vb 的應(yīng)該都知道left函數(shù),從字符串左邊取 n 個(gè)字符,但是不足是將全角、半角均視為是一個(gè)字符,造成在中英文混排的版面中不能截取等長(zhǎng)的字符串
作用:從字符串左邊截取 n 個(gè)字符,并支持全角半角字符的區(qū)分
實(shí)現(xiàn)方法:
String.prototype.left = function(num,mode){
if(!/\d+/.test(num))return(this);
var str = this.substr(0,num);
if(!mode) return str;
var n = str.Tlength() - str.length;
num = num - parseInt(n/2);
return this.substr(0,num);
}
試驗(yàn):
alert("EaseWe空間Spaces".left(8)) -> 顯示 EaseWe空間
alert("EaseWe空間Spaces".left(8,true)) -> 顯示 EaseWe空
本方法用到了上面所提到的String.Tlength()方法,自定義方法之間也能組合出一些不錯(cuò)的新方法呀!
(2) Date.DayDiff()
作用:計(jì)算出兩個(gè)日期型變量的間隔時(shí)間(年、月、日、周)
實(shí)現(xiàn)方法:
Date.prototype.DayDiff = function(cDate,mode){
try{
cDate.getYear();
}catch(e){
return(0);
}
var base =60*60*24*1000;
var result = Math.abs(this - cDate);
switch(mode){
case "y":
result/=base*365;
break;
case "m":
result/=base*365/12;
break;
case "w":
result/=base*7;
break;
default:
result/=base;
break;
}
return(Math.floor(result));
}
試驗(yàn):alert((new Date()).DayDiff((new Date(2002,0,1)))) -> 顯示 329
alert((new Date()).DayDiff((new Date(2002,0,1)),"m")) -> 顯示 10
當(dāng)然,也可以進(jìn)一步擴(kuò)充,得出響應(yīng)的小時(shí)、分鐘,甚至是秒。
(3) Number.fact()
作用:某一數(shù)字的階乘
實(shí)現(xiàn)方法:
Number.prototype.fact=function(){
var num = Math.floor(this);
if(num<0)return NaN;
if(num==0 || num==1)
return 1;
else
return (num*(num-1).fact());
}
試驗(yàn):alert((4).fact()) -> 顯示 24
這個(gè)方法主要是說(shuō)明了遞歸的方法在 prototype 方法中也是可行的!
JavaScript能夠?qū)崿F(xiàn)的面向?qū)ο蟮奶卣饔校?BR>·公有屬性(public field)
·公有方法(public Method)
·私有屬性(private field)
·私有方法(private field)
·方法重載(method overload)
·構(gòu)造函數(shù)(constructor)
·事件(event)
·單一繼承(single inherit)
·子類(lèi)重寫(xiě)父類(lèi)的屬性或方法(override)
·靜態(tài)屬性或方法(static member)
例子一(JavaScript中允許添加行為的類(lèi)型):可以在類(lèi)型上使用proptotype來(lái)為類(lèi)型添加行為。這些行為只能在類(lèi)型的實(shí)例上體現(xiàn)。 JS中允許的類(lèi)型有Array, Boolean, Date, Enumerator, Error, Function, Number, Object, RegExp, String
<script type="text/javascript">
Object.prototype.Property = 1;
Object.prototype.Method = function ()
{
alert(1);
}
var obj = new Object();
alert(obj.Property);
obj.Method();
</script>
<script type="text/javascript">
Object.prototype.Property = 1;
Object.prototype.Method = function (){ alert(1);}
var obj = new Object();
alert(obj.Property);
obj.Method();
</script>
例子二(prototype使用的限制):在實(shí)例上不能使用prototype,否則發(fā)生編譯錯(cuò)誤
<script type="text/javascript">
var obj = new Object();
obj.prototype.Property = 1; //Error
//Error
obj.prototype.Method = function()
{
alert(1);
}
</script>
<script type="text/javascript">var obj = new Object();obj.prototype.Property = 1; //Error//Errorobj.prototype.Method = function(){ alert(1);}</script>
例子三(如何定義類(lèi)型上的靜態(tài)成員):可以為類(lèi)型定義“靜態(tài)”的屬性和方法,直接在類(lèi)型上調(diào)用即可
<script type="text/javascript">
Object.Property = 1;
Object.Method = function()
{
alert(1);
}
alert(Object.Property);
Object.Method();
</script>
<script type="text/javascript">Object.Property = 1;Object.Method = function(){ alert(1);} alert(Object.Property);Object.Method();</script>
例子五():這個(gè)例子演示了通常的在JavaScript中定義一個(gè)類(lèi)型的方法
<script type="text/javascript">
function Aclass()
{
this.Property = 1;
this.Method = function()
{
alert(1);
}
}
var obj = new Aclass();
alert(obj.Property);
obj.Method();
</script>
<script type="text/javascript">function Aclass(){this.Property = 1;this.Method = function(){ alert(1);}}var obj = new Aclass();alert(obj.Property);obj.Method();</script>
例子六(JavaScript中允許添加行為的類(lèi)型):可以在外部使用prototype為自定義的類(lèi)型添加屬性和方法。
<script type="text/javascript">
function Aclass()
{
this.Property = 1;
this.Method = function()
{
alert(1);
}
}
Aclass.prototype.Property2 = 2;
Aclass.prototype.Method2 = function
{
alert(2);
}
var obj = new Aclass();
alert(obj.Property2);
obj.Method2();
</script>
<script type="text/javascript">function Aclass(){this.Property = 1;this.Method = function(){ alert(1);}}Aclass.prototype.Property2 = 2;Aclass.prototype.Method2 = function{ alert(2);}var obj = new Aclass();alert(obj.Property2);obj.Method2();</script>
例子八():可以在對(duì)象上改變屬性。(這個(gè)是肯定的)也可以在對(duì)象上改變方法。(和普遍的面向?qū)ο蟮母拍畈煌?BR>
<script type="text/javascript">
function Aclass()
{
this.Property = 1;
this.Method = function()
{
alert(1);
}
}
var obj = new Aclass();
obj.Property = 2;
obj.Method = function()
{
alert(2);
}
alert(obj.Property);
obj.Method();
</script>
<script type="text/javascript">function Aclass(){this.Property = 1;this.Method = function(){ alert(1);}}var obj = new Aclass();obj.Property = 2;obj.Method = function(){ alert(2);}alert(obj.Property);obj.Method();</script>
例子九():可以在對(duì)象上增加屬性或方法
<script type="text/javascript">
function Aclass()
{
this.Property = 1;
this.Method = function()
{
alert(1);
}
}
var obj = new Aclass();
obj.Property = 2;
obj.Method = function()
{
alert(2);
}
alert(obj.Property);
obj.Method();
</script>
<script type="text/javascript">function Aclass(){this.Property = 1;this.Method = function(){ alert(1);}}var obj = new Aclass();obj.Property = 2;obj.Method = function(){ alert(2);}alert(obj.Property);obj.Method();</script>
例子十(如何讓一個(gè)類(lèi)型繼承于另一個(gè)類(lèi)型):這個(gè)例子說(shuō)明了一個(gè)類(lèi)型如何從另一個(gè)類(lèi)型繼承。
<script type="text/javascript">
function AClass()
{
this.Property = 1;
this.Method = function()
{
alert(1);
}
}
function AClass2()
{
this.Property2 = 2;
this.Method2 = function()
{
alert(2);
}
}
AClass2.prototype = new AClass();
var obj = new AClass2();
alert(obj.Property);
obj.Method();
alert(obj.Property2);
obj.Method2();
</script>
<script type="text/javascript">function AClass(){ this.Property = 1; this.Method = function() { alert(1); }} function AClass2(){ this.Property2 = 2; this.Method2 = function() { alert(2); }}AClass2.prototype = new AClass(); var obj = new AClass2();alert(obj.Property);obj.Method();alert(obj.Property2);obj.Method2();</script>
例子十一(如何在子類(lèi)中重新定義父類(lèi)的成員):這個(gè)例子說(shuō)明了子類(lèi)如何重寫(xiě)父類(lèi)的屬性或方法。
<script type="text/javascript">
function AClass()
{
this.Property = 1;
this.Method = function()
{
alert(1);
}
}
function AClass2()
{
this.Property2 = 2;
this.Method2 = function()
{
alert(2);
}
}
AClass2.prototype = new AClass();
AClass2.prototype.Property = 3;
AClass2.prototype.Method = function()
{
alert(4);
}
var obj = new AClass2();
alert(obj.Property);
obj.Method();
</script>
- js中繼承的幾種用法總結(jié)(apply,call,prototype)
- js中的hasOwnProperty和isPrototypeOf方法使用實(shí)例
- JavaScript prototype 使用介紹
- 詳解Javascript中prototype屬性(推薦)
- Javascript中Array.prototype.map()詳解
- JQuery,Extjs,YUI,Prototype,Dojo 等JS框架的區(qū)別和應(yīng)用場(chǎng)景簡(jiǎn)述
- 判斷js中各種數(shù)據(jù)的類(lèi)型方法之typeof與0bject.prototype.toString講解
- JavaScript中的prototype使用說(shuō)明
- JavaScript函數(shù)及其prototype詳解
相關(guān)文章
JavaScript登錄記住密碼操作(超簡(jiǎn)單代碼)
本文給大家分享一段簡(jiǎn)單的js代碼實(shí)現(xiàn)用戶登錄記住密碼操作,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-03-03chrome下判斷點(diǎn)擊input上標(biāo)簽還是其余標(biāo)簽的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇chrome下判斷點(diǎn)擊input上標(biāo)簽還是其余標(biāo)簽的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09CSDN上快速結(jié)貼的方法,JS實(shí)現(xiàn)
CSDN上快速結(jié)貼的方法,JS實(shí)現(xiàn)...2007-03-03Javascript執(zhí)行流程細(xì)節(jié)原理解析
這篇文章主要介紹了Javascript執(zhí)行流程細(xì)節(jié)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05js如何準(zhǔn)確獲取當(dāng)前頁(yè)面url網(wǎng)址信息
這篇文章主要為大家介紹了js準(zhǔn)確獲取當(dāng)前頁(yè)面url網(wǎng)址信息的多種方法,包括正則法、split拆分法等,需要的朋友可以參考下2016-04-04JavaScript常見(jiàn)繼承模式實(shí)例小結(jié)
這篇文章主要介紹了JavaScript常見(jiàn)繼承模式,結(jié)合實(shí)例形式總結(jié)分析了javascript原型鏈繼承、構(gòu)造函數(shù)繼承、組合繼承、原型式繼承、寄生式繼承等相關(guān)實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下2019-01-01postman+json+springmvc測(cè)試批量添加實(shí)例
下面小編就為大家分享一篇postman+json+springmvc測(cè)試批量添加實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03