命名空間:
Type.registerNamespace("MyNamespance");
--註冊一個命名空間
類:
定義步驟
--定義建構函式
--定義成員(方法,屬性,事件)
--註冊類
建構函式
類的建構函式即為function定義
通常用於初始化域變數
私人成員使用底線開頭(無法真正封裝)
--this._name
--this._age
MyNamespace.MyClass =functin(name,age)
{
this._name = name;
this._age = age;
}
定義方法:
基於prototype定義
-- MyNamespace.MyClass.prototype=
{
myMethod1:function()
{
},
myMethod2:function()
{
this.myMethod();//this保留字不可少
}
}
定義屬性
--Microsoft AJAX Libray的物件導向類型系統set_和get_開頭的方法認作屬性
--避免定義唯寫屬性,使用某個方法替代
MyNamespace.MyClass.prototype =
{
get_name:function()
{
retrun this._name;
},
set_name:funciton(value)
{
this._name = value;
}
}
註冊類:
MyNamespace.MyClass.registerClass("MyNamespace.MyClass");
抽象類別:
包含抽象方法的類即為抽象類別
MyNamespace.MyClass.prototype=
{
myMthod:function()
{
throw Error.notImplemented();
}
}
繼承:
調用父類的建構函式
--沒有父類的類可以省略這步
--有父類的類必須調用父類的建構函式,否則會丟失繼承效果
MyNamespace.MyClass = function()
{
//調用父類的建構函式
MyNamespace.MyClass.initializeBase(this,[parm1,......]);
}
註冊類時提供父類
可以直接以普通方式實現父類的抽象成員
toString()方法無法被繼承
//MyClass 繼承與MyParentClass
MyNamespace.MyClass.registerClass("MyNamespace.Myclass",MyNamespace,MyParentClass);
調用父類的方法
Namespace.MyClass.prototype=
{
myMethod:function(parm1,...)
{
//調用父類的myMethod方法
MyNamespace.MyClass.callBaseMethod(this,"myMethod",[parm1,...]);
}
}
繼承時需要注意的問題
toString()方法無法繼承.同樣的還有toLocaleString,ValueOf,hasOwnProperty等方法
繼承的原理是通過for迴圈遍曆父類的方法複製到子類實現繼承效果但是for迴圈無法遍曆出"toString", "toLocaleString",
"valueOf", "hasOwnProperty", "isPrototypeOf","propertyIsEnumerable"
這個幾個方法所以要手動的修改 ASP.NET AJAX Library 的函數
解決方案:
在你的javascript指令碼添加這段script指令碼來覆蓋ASP.NET AJAX Library的函數
1Type.prototype.resolveInheritance = function Type$resolveInheritance() {
2 if (arguments.length !== 0) throw Error.parameterCount();
3
4 if (this.__basePrototypePending) {
5 var baseType = this.__baseType;
6
7 baseType.resolveInheritance();
8
9 for (var memberName in baseType.prototype) {
10 var memberValue = baseType.prototype[memberName];
11 if (!this.prototype[memberName]) {
12 this.prototype[memberName] = memberValue;
13 }
14 }
15
16 var dontEnumMembers = ["toString", "toLocaleString",
17 "valueOf", "hasOwnProperty", "isPrototypeOf",
18 "propertyIsEnumerable"];
19
20 for (var i = 0; i < dontEnumMembers.length; i++)
21 {
22 var memberName = dontEnumMembers[i];
23 if (this.prototype[memberName] != Object.prototype[memberName])
24 {
25 continue;
26 }
27
28 var memberValue = baseType.prototype[memberName];
29 if (memberValue != Object.prototype[memberName])
30 {
31 this.prototype[memberName] = memberValue;
32 }
33 }
34
35 delete this.__basePrototypePending;
36 }
37 }
38
39
介面:
與類的定義方法大致相同
建構函式拋出異常
所有的方法拋出異常
註冊介面時使用registerInterface方法
介面無法繼承於其它介面
由於JavaScript為弱類型語言,因此Microsoft AJAX Library 中介面的作用只是標記,可以從某些反射方法中反應出效果
MyNamespace.IMyInterface = funciton()
{
throw Error.notImplemented();
}
MyNamespace.IMyInterface.prototype =
{
Method:function()
{
throw Error.notImplemented();
}
}
實現介面
在使用registerClass方法註冊類時可以傳入額外的參數來實現介面
MyNamespace.MyClass.registerClass("MyNamespace.MyClass",/**父類 可以為null**/,/**介面**/,/**介面**/,/**介面**/);
枚舉
枚舉即為Number
增加可讀性
可定義為標記(Flag) 使用標記註冊枚舉時提供第二個參數為 true :MyNamespace.MyEnum.registerEnum("MyNamespace.MyEnum",true);
每個枚舉均有toString和parse方法
枚舉註冊使用registerEnum來註冊
得到枚舉項的字串表表示需使用 MyNamespace.MyEnum.toString(枚舉的項);
parse的使用方法: MyNamespace.myEnum.parse("枚舉的字串");
MyNamespace.MyEnum=function()
{
throw Error.notImplemented();
}
MyNamespace.MyEnum.prototype=
{
item1 : 1,
item2 : 2,
imte3 : 3
}
MyNamespace.MyEnum.registerEnum("MyNamespace.MyEnum");
反射方法
Type.prototype
getBaseType()
//返回執行個體的基底類型。
getInterfaces()
//返回一個 Array 對象,該對象包含類型所實現的介面的列表。
getName()
//返回執行個體類型的名稱。
implementsInterface(interface)
//確定此類型是否實現了指定介面。interface:要測試介面
inheritsFrom(parentType)
//確定此類型是否從指定的父類型繼承。parentType 要作為當前執行個體的基類進行測試的類的完全限定名。
isImplementedBy(typeInstanceVar)
//確定執行個體是否實現了指定介面。typeInstanceVar 在其上測試該介面的執行個體。
isInstanceOfType(instance)
//確定對象是否為指定類型或其某個衍生類別型的執行個體。instance 要測試的對象。
getRootNamespaces()
//返回一個 Array 對象,該對象包含對用戶端應用程式的所有根命名空間的引用。
Type.isClass(type)
//返回一個值,該值指示指定的類型是否為類。
--type 要測試的類型。
Type.isInterface(type)
//返回一個值,該值指示指定的類型是否為介面。
--type 要測試的類型。
Type.isNamespace(object)
//返回一個值,該值指示指定對象是否為命名空間。
--object 要測試的對象。
Type.isEnum(type)
//指示指定的類型是否為枚舉。
--type 要測試的類型。
Type.isFlags(type)
//擷取一個值,該值指示指定的類型是否為標誌的整數。
--type 要測試的類型。
Type.parse(typeName,ns)
//返回通過類型名稱指定的類型的一個執行個體。
--typeName 一個表示完全限定類名稱的字串。可以為 null。
--ns (可選)包含該類的命名空間。
事件
定義EventHandlerList對象
MyNamespace.MyClass = function()
{
this._events = new Sys.EventHandlerList();
}
添加add和reomve 事件
MyNamespace.MyClass.prototype=
{
add_myEvent:function(handler)
{
this._events.addHandler("myEvent",handler);
},
remove_myEvent:function(handler)
{
this._evnets.remove("myEvent",handler);
}
}
添加觸發事件的方法
MyNamespace.MyClass.prototype=
{
raiseMyEvent:function(e)
{
var handler = this._events.getHander("myEvent");
if(handler)
{
handler(this.e);
}
}
}