對於 Microsoft AJAX Library 擴充的 JavaScript ,其具有發射功能—Reflection。
反射是在運行時去驗證應用程式的結構和組件的能力。
在 ASP.NET AJAX 中同樣是使用的 Global 下的 Type 類來實現的反射。
開發人員可以通過反射來收集對象的形象,比如對象繼承自那個類,
對象是那個類的執行個體,以及對象是否實現了指定的介面等等資訊。
樣本相對簡單,無非就是 Type 下的幾個方法的使用,
比如 inheritsFrom 等等。
HTML 標籤
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<script language="javascript" type="text/javascript">
Type.registerNamespace("BaoBeiMe");
//先註冊一個父類
BaoBeiMe.FatherClass = function(firstName, lastName) {
this._firstName = firstName;
this._lastName = lastName;
}
BaoBeiMe.FatherClass.prototype = {
get_firstName: function() {
return this._firstName;
},
set_fisrtName: function(name) {
this._firstName = name;
},
set_lastName: function(name) {
this._lastName = name;
},
get_lastName: function() {
return this._lastName;
},
toString: function() {
return String.format("Hello, {0}{1}",
this.get_firstName(), this.get_lastName());
}
}
BaoBeiMe.FatherClass.registerClass("BaoBeiMe.FatherClass", null, null);
//再註冊一個子類,繼承自 BaoBeiMe.FatherClass
BaoBeiMe.ChildClass = function(firstName, lastName, Sex) {
BaoBeiMe.ChildClass.initializeBase(this, [firstName, lastName]);
this._sex = Sex;
}
BaoBeiMe.ChildClass.prototype = {
set_sex: function(sex) {
this._sex = sex;
},
get_sex: function() {
return this._sex;
},
toString: function() {
return BaoBeiMe.ChildClass.callBaseMethod(this, "toString") +
" , 性別為 " + this.get_sex();
}
}
BaoBeiMe.ChildClass.registerClass("BaoBeiMe.ChildClass",
BaoBeiMe.FatherClass, null);
//再註冊一個介面
BaoBeiMe.MyInterface = function() { }
BaoBeiMe.MyInterface.prototype = {
IGetMsg: function() { }
}
BaoBeiMe.MyInterface.registerInterface("BaoBeiMe.MyInterface");
//再註冊一個子類,其繼承自 BaoBeiMe.ChildClass ,
//並且實現了介面 BaoBeiMe.MyInterface
BaoBeiMe.ChildClassTwo = function(firstName, lastName, sex, age) {
BaoBeiMe.ChildClassTwo.initializeBase(this, [firstName, lastName, sex]);
this._age = age;
}
BaoBeiMe.ChildClassTwo.prototype = {
get_age: function() {
return this._age;
},
set_age: function(age) {
this._age = age;
},
toString: function() {
return BaoBeiMe.ChildClassTwo.callBaseMethod(this, "toString") +
" , 年齡為 " + this.get_age();
},
IGetMsg: function() {
return "在這裡調用了介面中的方法 IGetMsg";
}
}
BaoBeiMe.ChildClassTwo.registerClass("BaoBeiMe.ChildClassTwo",
BaoBeiMe.ChildClass, BaoBeiMe.MyInterface);
//再自訂一個類,這個類不繼承任何類,也不實現任何介面
BaoBeiMe.MyClass = function(myName) {
this._myName = myName;
}
BaoBeiMe.MyClass.prototype = {
set_myName: function(myName) {
this._myName = myName;
},
get_myName: function() {
return this._myName;
},
toString: function() {
return String.format("Hello,I'm {0}", this.get_myName());
}
}
BaoBeiMe.MyClass.registerClass("BaoBeiMe.MyClass", null, null);
var fatherClass;
var childClass;
var childClassTwo;
var myClass;
function Button1_onclick() {
fatherClass = new BaoBeiMe.FatherClass("BaoBei", "Me");
childClass = new BaoBeiMe.ChildClass("Xiao", "Zhen", "男")
childClassTwo = new BaoBeiMe.ChildClassTwo("BaoBeiMe", "XiaoZhen", "男", "20");
myClass = new BaoBeiMe.MyClass("XiaoZhen");
var lblFatherClass = $get('<%=lblFatherClass.ClientID %>');
var lblChildClass = $get('<%=lblChildClass.ClientID %>');
var lblChildClassTwo = $get('<%=lblChildClassTwo.ClientID %>');
var lblMyClass = $get('<%=lblMyClass.ClientID %>');
lblFatherClass.innerText = fatherClass.toString();
lblChildClass.innerText = childClass.toString();
lblChildClassTwo.innerText = childClassTwo.toString();
lblMyClass.innerText = myClass.toString();
}
function Button2_onclick() {
var lblChildClassFromFatherClass =
$get('<%=lblChildClassFromFatherClass.ClientID %>');
var lblChildClassTwoFromFatherClass =
$get('<%=lblChildClassTwoFromFatherClass.ClientID %>');
var lblChildClassTwoFromChildClass =
$get('<%=lblChildClassTwoFromChildClass.ClientID %>');
var lblMyClassTwoFromFatherClass =
$get('<%=lblMyClassTwoFromFatherClass.ClientID %>');
lblChildClassFromFatherClass.innerText =
BaoBeiMe.ChildClass.inheritsFrom(BaoBeiMe.FatherClass);
lblChildClassTwoFromFatherClass.innerText =
BaoBeiMe.ChildClassTwo.inheritsFrom(BaoBeiMe.FatherClass);
lblChildClassTwoFromChildClass.innerText =
BaoBeiMe.ChildClassTwo.inheritsFrom(BaoBeiMe.ChildClass);
lblMyClassTwoFromFatherClass.innerText =
BaoBeiMe.MyClass.inheritsFrom(BaoBeiMe.ChildClass);
}
function Button3_onclick() {
var lblFatherClassInterface =
$get('<%=lblFatherClassInterface.ClientID %>');
var lblChildClassInterface =
$get('<%=lblChildClassInterface.ClientID %>');
var lblChildClassTwoInterface =
$get('<%=lblChildClassTwoInterface.ClientID %>');
var lblMyClassInterface =
$get('<%=lblMyClassInterface.ClientID %>');
lblFatherClassInterface.innerText =
BaoBeiMe.FatherClass.implementsInterface(BaoBeiMe.MyInterface);
lblChildClassInterface.innerText =
BaoBeiMe.ChildClass.implementsInterface(BaoBeiMe.MyInterface);
lblChildClassTwoInterface.innerText =
BaoBeiMe.ChildClassTwo.implementsInterface(BaoBeiMe.MyInterface);
lblMyClassInterface.innerText =
BaoBeiMe.MyClass.implementsInterface(BaoBeiMe.MyInterface);
}
function Button4_onclick() {
var lblFatherClassToFatherClass =
$get('<%=lblFatherClassToFatherClass.ClientID %>');
var lblMyClassClassToFatherClass =
$get('<%=lblMyClassClassToFatherClass.ClientID %>');
var lblChildClassToChildClass =
$get('<%=lblChildClassToChildClass.ClientID %>');
lblFatherClassToFatherClass.innerText =
BaoBeiMe.FatherClass.isInstanceOfType(fatherClass);
lblMyClassClassToFatherClass.innerText =
BaoBeiMe.FatherClass.isInstanceOfType(myClass);
lblChildClassToChildClass.innerText =
BaoBeiMe.ChildClass.isInstanceOfType(childClass);
}
</script>
<div>
<input id="Button1" type="button"
value="執行個體化 FatherClass,ChildClass,ChildClassTwo 類"
onclick="return Button1_onclick()" /><br />
<br />
調用 FatherClass 的 toString() 後的結果
<asp:Label ID="lblFatherClass" runat="server" Text=""></asp:Label>
<br />
<br />
調用 ChildClass 的 toString() 後的結果
<asp:Label ID="lblChildClass" runat="server" Text=""></asp:Label>
<br />
<br />
調用 ChildClassTwo 的 toString() 後的結果
<asp:Label ID="lblChildClassTwo" runat="server" Text=""></asp:Label>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<br />
<br />
調用 MyClass 的 toString() 後的結果
<asp:Label ID="lblMyClass" runat="server" Text=""></asp:Label>
<br />
<br />
<input id="Button2" type="button" value="判斷繼承關係"
onclick="return Button2_onclick()" /><br />
<br />
判斷 ChildClass 是否繼承自 FatherClass 類
<asp:Label ID="lblChildClassFromFatherClass" runat="server" Text="">
</asp:Label>
<br />
<br />
判斷 ChildClassTwo 是否繼承自 FatherClass 類
<asp:Label ID="lblChildClassTwoFromFatherClass" runat="server" Text="">
</asp:Label>
<br />
<br />
判斷 ChildClassTwo 是否繼承自 ChildClass 類
<asp:Label ID="lblChildClassTwoFromChildClass" runat="server" Text="">
</asp:Label>
<br />
<br />
判斷 MyClass 是否繼承自 FatherClass 類
<asp:Label ID="lblMyClassTwoFromFatherClass" runat="server" Text="">
</asp:Label>
<br />
<br />
<input id="Button3" type="button" value="判斷是否實現介面"
onclick="return Button3_onclick()" /><br />
<br />
判斷 FatherClass 是否實現了 MyInterface 介面
<asp:Label ID="lblFatherClassInterface" runat="server" Text="">
</asp:Label>
<br />
<br />
判斷 ChildClass 是否實現了 MyInterface 介面
<asp:Label ID="lblChildClassInterface" runat="server" Text="">
</asp:Label>
<br />
<br />
判斷 ChildClassTwo 是否實現了 MyInterface 介面
<asp:Label ID="lblChildClassTwoInterface" runat="server" Text="">
</asp:Label>
<br />
<br />
判斷 MyClass 是否實現了 MyInterface 介面
<asp:Label ID="lblMyClassInterface" runat="server" Text="">
</asp:Label>
<br />
<br />
<input id="Button4" type="button" value="判斷一個執行個體是否為指定類的對象"
onclick="return Button4_onclick()" />
<br />
<br />
判斷 fatherClass 是否為 FatherClass類的執行個體
<asp:Label ID="lblFatherClassToFatherClass" runat="server" Text="">
</asp:Label>
<br />
<br />
判斷 childClass 是否為 FatherClass類的執行個體
<asp:Label ID="lblMyClassClassToFatherClass" runat="server" Text="">
</asp:Label>
<br />
<br />
判斷 myClass 是否為 ChildClass類的執行個體
<asp:Label ID="lblChildClassToChildClass" runat="server" Text="">
</asp:Label>
<br />
<br />
</div>
</form>
</body>
</html>
結果
簡單將來,在 JavaScript 中的反射無非就是那麼幾個方法的使用,
所以比較簡單,只是要明白其中的內容卻並不是很容易的。
2010—1—25