命名空间:
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的函数
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);
}
}
}