js dom高級程序設計學習筆記
1、建立可重用的對象
- 對象分爲兩種:function對象和object對象。
- 向function對象添加靜態屬性和方法,僅對該構造函數才能訪問,對其實例是沒法訪問的。要想添加公有屬性和方法,需使用Prototype,使用prototype定義的屬性和方法,這樣定義的方法是不能經過構造函數訪問的,必須經過實例訪問。
- 在構造函數中經過var 定義的變量和和function直接聲明的方法就是私有方法和屬性。要想訪問私有屬性和方法,就必須在構造函數內用this關鍵字定義的特權方法。特權方法是共有方法。
function MyConsturctor(mesg){
this.mesg = mesg;
// 私有屬性
var separator = '';
var myOwner = this;// this指向實例
// 私有方法
function alertMesg() {
alert(myOwner.mesg);
}
// 特權方法
this.appendTomesg = function(string) {
this.mesg += separator + string;
alertMesg();
}
}
// 公有方法
MyConstructor.prototype.clearMesg = function() {
this.mesg = '';
}
// 靜態屬性
MyConstructor.name = 'jeff';
// 靜態方法
MyConstructor.alertName = function() {
alert(this.name);
}
2、Ajax