js中關於prototype屬性

<script language=javascript>
function Person(name,sex) {  //Person類的構造函數
    this.name = name;
    this.sex = sex;
}

Person.prototype.age = 12;   //爲Person類的prototype屬性對應的prototype對象的屬性賦值,
                             //相當於爲Person類的父類添加屬性
Person.prototype.print = function() { //爲Person類的父類添加方法
    alert(this.name+"_"+this.sex+"_"+this.age);
};

var p1 = new Person("name1","male"); //p1的age屬性繼承子Person類的父類(即prototype對象)
var p2 = new Person("name2","male");

p1.print();  //name1_male_12
p2.print();  //name2_male_12

p1.age = 34; //改變p1實例的age屬性   p1屬性在不指向Person的prototype屬性
p1.print();  //name1_male_34
p2.print();  //name2_male_12

Person.prototype.age = 22;  //改變Person類的超類的age屬性
p1.print();  //name1_male_34(p1的age屬性並沒有隨着prototype屬性的改變而改變)
p2.print();  //name2_male_22(p2的age屬性發生了改變)

p1.print = function() {  //改變p1對象的print方法
    alert("i am p1");
}

p1.print();  //i am p1(p1的方法發生了改變)
p2.print();  //name2_male_22(p2的方法並沒有改變)

Person.prototype.print = function() { //改變Person超類的print方法
    alert("new print method!");
}

p1.print();  //i am p1(p1的print方法仍舊是自己的方法)
p2.print();  //new print method!(p2的print方法隨着超類方法的改變而改變)

</script>

 

JS創建類有集中方法,我個人比較喜歡的方式是“混合的構造函數/原型方式”。比較好理解
用構造函數來定義非函數屬性,用原型方式定義對象的函數屬性,結果所有函數鬥只創建一次,而每個對象鬥具有自由的對象屬性實例。
 

 function ocar(color){
  this.color = color;
  this.arr = new Array("s");
 }
 ocar.prototype.showColor = function(){
  alert(this.color);
 }
 var car = new ocar("resd");
 car.showColor();
 
二、爲類添加新方法:
可以用prototype屬性爲以有的類定義新的方法:
比如爲Array定義一個dequeue()方法
//創建新的方法
Array.prototype.dequeue = function(str){
 this.push(str);
}
var arr = new Array("s");
arr.dequeue("a");
alert(arr.toString());
 
三、重定義已有的方法:
就相給已有類定義新方法一樣,也可以重寫類的方法。函數名只是指向函數的指針,因此可以輕易的使用它指向別的函數。從寫已有方法的時候Function的第一個F要大寫
修改本地類toString()方法。
F unction.prototype.toString = function(){
 return "重寫toString";
}
function sayHi(){
 alert("Hi");
}
alert(sayHi.toString);
 
四、類的繼承:
JS類的繼承有很多種,這因爲JS種的繼承機制並不是明確規定的,而是通過模仿實現的,這意味着所有的繼承細節並不是完全解釋程序處理。所以我們選擇一種適合自己的方法就可以了。
一、對象冒充
   構造函數使用shis關鍵字給所有屬性和方法賦值,因爲構造類只是一種函數,所以可以使ClassA的構造函數成爲ClassB的方法,然後調用它,ClassB就會收到ClassA的構造函數中定義的屬性和方法。例如
function oren(name){
 this.name = name;
 this.sayName = function(){
  alert(this.name);
 }
}
function orenB(name,sex){
 this.newfun = oren;
 this.newfun(name);
 delete this.newfun;
 this.sex = sex;
 this.showSex = function(){
  alert(this.sex);
 }
}
var testA = new oren("linan");
testA.sayName();
var testB = new orenB("ln","男");
testB.sayName();
testB.showSex();
所有的新屬性和新方法都必須在刪除了新方法的代碼後定義。否則會覆蓋超類的相關屬性和方法。
 
二、call()方法:
call()方法是與經典的對象冒充方法最相似的方法。它的第一個參數用作this的對象,其他參都直接傳遞給函數本身。
function oren(name){
 this.name = name;
 this.sayName = function(){
  alert(this.name);
 }
}
function orenB(name,sex){
 oren.call(this,name);
 this.sex = sex;
 this.getSex = function(){
  alert(this.sex);
 }
}
var test = new oren("ln");
test.sayName();
var testB = new orenB("linan","man");
testB.sayName();
testB.getSex();
 
這是call()方法繼承的例子,這裏想讓oren中的關鍵字this等於新創建的orenB對象,因此this是第一個參數。

prototype 是在 IE 4 及其以後版本引入的一個針對於某一類的對象的方法,而且特殊的地方便在於:它是一個給類的對象添加方法的方法!這一點可能聽起來會有點亂,別急,下面我便通過實例對這一特殊的方法作已下講解:

  首先,我們要先了解一下類的概念,JavaScript 本身是一種面向對象的語言,它所涉及的元素根據其屬性的不同都依附於某一個特定的類。我們所常見的類包括:數組變量(Array)、邏輯變量 (Boolean)、日期變量(Date)、結構變量(Function)、數值變量(Number)、對象變量(Object)、字符串變量 (String) 等,而相關的類的方法,也是程序員經常用到的(在這裏要區分一下類的注意和屬性發方法),例如數組的push方法、日期的get系列方法、字符串的 split方法等等,

  但是在實際的編程過程中不知道有沒有感覺到現有方法的不足?prototype 方法應運而生!下面,將通過實例由淺入深講解 prototype 的具體使用方法:


1、最簡單的例子,瞭解 prototype:
(1) Number.add(num):作用,數字相加
實現方法:Number.prototype.add = function(num){return(this+num);}
試驗:alert((3).add(15)) -> 顯示 18


(2) Boolean.rev(): 作用,布爾變量取反
實現方法:Boolean.prototype.rev = function(){return(!this);}
試驗:alert((true).rev()) -> 顯示 false

是不是很簡單?這一節僅僅是告訴讀者又這麼一種方法,這種方法是這樣運用的。


2、已有方法的實現和增強,初識 prototype:
(1) Array.push(new_element)
  作用:在數組末尾加入一個新的元素
  實現方法:
  Array.prototype.push = function(new_element){
         this[this.length]=new_element;
         return this.length;
     }
  讓我們進一步來增強他,讓他可以一次增加多個元素!
  實現方法:
  Array.prototype.pushPro = function() {
         var currentLength = this.length;
         for (var i = 0; i < arguments.length; i++) {
             this[currentLength + i] = arguments[i];
         }
         return this.length;
     }
  應該不難看懂吧?以此類推,你可以考慮一下如何通過增強 Array.pop 來實現刪除任意位置,任意多個元素(具體代碼就不再細說了)

(2) String.length
  作用:這實際上是 String 類的一個屬性,但是由於 JavaScript 將全角、半角均視爲是一個字符,在一些實際運用中可能會造成一定的問題,現在我們通過 prototype 來彌補這部不足。
  實現方法:
  String.prototype.cnLength = function(){
         var arr=this.match(/[^/x00-/xff]/ig);
         return this.length+(arr==null?0:arr.length);
     }
  試驗:alert("EaseWe空間Spaces".cnLength()) -> 顯示 16
  這裏用到了一些正則表達式的方法和全角字符的編碼原理,由於屬於另兩個比較大的類別,本文不加說明,請參考相關材料。


3、新功能的實現,深入 prototype:在實際編程中所用到的肯定不只是已有方法的增強,更多的實行的功能的要求,下面我就舉兩個用 prototype 解決實際問題的例子:
(1) String.left()
  問題:用過 vb 的應該都知道left函數,從字符串左邊取 n 個字符,但是不足是將全角、半角均視爲是一個字符,造成在中英文混排的版面中不能截取等長的字符串
  作用:從字符串左邊截取 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);
     }
  試驗:
     alert("EaseWe空間Spaces".left(8)) -> 顯示 EaseWe空間
     alert("EaseWe空間Spaces".left(8,true)) -> 顯示 EaseWe空
  本方法用到了上面所提到的String.Tlength()方法,自定義方法之間也能組合出一些不錯的新方法呀!

(2) Date.DayDiff()
  作用:計算出兩個日期型變量的間隔時間(年、月、日、周)
  實現方法:
  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));
     }
  試驗:alert((new Date()).DayDiff((new Date(2002,0,1)))) -> 顯示 329
     alert((new Date()).DayDiff((new Date(2002,0,1)),"m")) -> 顯示 10
  當然,也可以進一步擴充,得出響應的小時、分鐘,甚至是秒。

(3) Number.fact()
  作用:某一數字的階乘
  實現方法:
  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());
     }
  試驗:alert((4).fact()) -> 顯示 24
  這個方法主要是說明了遞歸的方法在 prototype 方法中也是可行的!
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章