接上篇http://www.cnblogs.com/Brave-Heart/archive/2008/06/25/1229839.html
介面
·建構函式拋出異常
·所有的方法拋出異常
·registerInterface方法用來註冊介面
·介面不能繼承其他的介面
·定義(和定義類很相似)
MyNamespace.IMyInterface = function()
{
throw Error.notImplemented();
}
MyNamespace.IMyInterface.prototype=
{
myMethod:function()
{
throw Error.notImplemented();
},
......
}
MyNamespace.IMyInterface.registerInterface("MyNamespace.IMyInterface");
·實現介面
使用registerClass方法傳入額外的參數實現介面
MyNamespace.MyClass.registerClass("MyNamespace.MyClass",null,MyNamespace.IMyInterface1,IMyInterface2,...);
//第二個參數null表示沒有父類
【code demo】:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<script language="javascript" type="text/javascript">
Type.registerNamespace("dongdeNamespace");
dongdeNamespace.Worker = function(name)
{
this._name = name ? name:"(unknown)";
this._bornyear = 0;
}
dongdeNamespace.Worker.prototype=
{
get_name:function()
{
return this._name;
},
get_bornyear:function()
{
return this._bornyear;
},
set_bornyear:function(value)
{
this._bornyear=value;
},
getAge:function()
{
return 2008-this._bornyear;
},
getWorkerDescript:function()
{
return String.format("{0}年齡:{1},出生於:{2}",this._name,this.getAge(),this.get_bornyear());
},
//抽象方法
getSalary:function()
{
throw Error.notImplemented();
}
}
//////////////////////////////////////////////////
dongdeNamespace.IWorker = function()
{
throw Error.notImplemented();
}
dongdeNamespace.IWorker.prototype=function()
{
getSex:function()
{
throw Error.notImplemented();
}
}
dongdeNamespace.IWorker.registerInterface("dongdeNamespace.IWorker");
dongdeNamespace.Worker.registerClass("dongdeNamespace.Worker",null,dongdeNamespace.IWorker);
//dongdeNamespace.Worker.registerClass("dongdeNamespace.Worker");
var newWorker = new dongdeNamespace.Worker();
alert( dongdeNamespace.IWorker.isImplementedBy(newWorker) ); //isImplementedBy反射方法,
判斷newWorker對象有沒有實現dongdeNamespace.IWorker介面
</script>
枚舉
·枚舉即為number
·很大的好處就是爭強可讀性,發現很多初寫程式的人表示某種狀態都是用數字1,2,3...來表示,這樣做代碼的可讀性不強,不好理解
·每個枚舉類型都有toString和parse方法
·定義
MyNamespace.MyEnum = function()
{
throw Error.notImplemented();
}
MyNamespace.MyEnum.prototype=
{
Item1: 1,
Item2: 2,
Item3: 3
}
MyNamespace.MyEnum.registerEnum("MyNamespace.MyEnum",[,true]);
【code demo】:稍微簡單的列子
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<script language="javascript" type="text/javascript">
Type.registerNamespace("MyNamespace");
MyNamespace.MyEnum = function()
{
throw Error.notImplemented();
}
MyNamespace.MyEnum.prototype=
{
Item1: "dongde",
Item2: "ling",
Item3: "mary"
}
MyNamespace.MyEnum.registerEnum("MyNamespace.MyEnum");
alert(MyNamespace.MyEnum.Item1);
alert(MyNamespace.MyEnum.toString(MyNamespace.MyEnum.Item1));
</script>
反射方法
Type.prototype.getBaseType
Type.prototype.getInterfaces
Type.prototype.getName
Type.prototype.implementsInterface
Type.prototype.inheritsFrom
Type.prototype.isImplementedBy
Type.prototype.isInstanceOfType
Type.getRootNamespaces
Type.isClass
Type.isInterface
Type.isNamespace
Type.isEnum
Type.isFlags
Type.parse
Object.getType
Object.getTypeName