javascript 中的 prototype
早上上班想說看看Ajax 的library 發現要先瞭解prototype 的使用,所以開始了prototype 的學習
使用 prototype 物件來增添屬性
在增添客制化的屬性時,使用 prototype 關鍵字在那個物件上。這樣一來,每一個該物件的實體均會有這個屬性。
例如:
//First, create the custom object “circle”
function circle(){
}
circle.prototype.pi=3.14159
使用 prototype 物件來增添方法
/*code for extending String object with method that writes text backwards*/
//core custom method for writing text backwards
function outputbackwards(){
for (i=this.length-1;i>=0;i--)
document.write(this.charAt(i));
}
//Attach custom method to string object
String.prototype.writeback=outputbackwards;
var message1="Danny";
message1.writeback();
</script>
Javascript 的學習網站