For JavaScript extended by Microsoft Ajax library, it has the launch function-reflection.
Reflection verifies the application at runtime.ProgramStructure and component capabilities.
In ASP. NET Ajax, the type class in global is also used for reflection.
Developers can use reflection to collect object images. For example, an object inherits from that class,
The object is the instance of that class, and whether the object implements the specified interface.
The example is relatively simple. It is nothing more than the use of several methods under type,
For example, inheritsfrom.
HTML Tag
<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 ");
// Register a parent class first
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 );
// Register another subclass and inherit from 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") +
", Gender:" + this. get_sex ();
}
}
Baobeime. childclass. registerclass ("baobeime. childclass ",
Baobeime. fatherclass, null );
// Register another interface
Baobeime. myinterface = function (){}
Baobeime. myinterface. Prototype = {
Igetmsg: function (){}
}
Baobeime. myinterface. registerinterface ("baobeime. myinterface ");
// Register another subclass, which inherits from baobeime. childclass,
// And implements the baobeime. myinterface Interface
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") +
", Age:" + this. get_age ();
},
Igetmsg: function (){
Return "The method igetmsg in the interface is called here ";
}
}
Baobeime. childclasstwo. registerclass ("baobeime. childclasstwo ",
Baobeime. childclass, baobeime. myinterface );
// Define another class. This class does not inherit any classes or implement any interfaces.
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", "male ")
Childclasstwo = new baobeime. childclasstwo ("baobeime", "Xiaozhen", "male", "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 = "instantiate fatherclass, childclass, childclasstwo class"
Onclick = "Return button1_onclick ()"/> <br/>
<Br/>
Result After fatherclass tostring () is called & nbsp;
<Asp: Label id = "lblfatherclass" runat = "server" text = ""> </ASP: Label>
<Br/>
<Br/>
Result After calling tostring () of childclass & nbsp;
<Asp: Label id = "lblchildclass" runat = "server" text = ""> </ASP: Label>
<Br/>
<Br/>
Result After calling tostring () of childclasstwo & nbsp;
<Asp: Label id = "lblchildclasstwo" runat = "server" text = ""> </ASP: Label>
<Asp: Label id = "label1" runat = "server" text = ""> </ASP: Label>
<Br/>
<Br/>
Result After calling tostring () of myclass & nbsp;
<Asp: Label id = "lblmyclass" runat = "server" text = ""> </ASP: Label>
<Br/>
<Br/>
<Input id = "button2" type = "button" value = "determine the inheritance relationship"
Onclick = "Return button2_onclick ()"/> <br/>
<Br/>
Determine whether childclass inherits from fatherclass Class & nbsp;
<Asp: Label id = "lblchildclassfromfatherclass" runat = "server" text = "">
</ASP: Label>
<Br/>
<Br/>
Determine whether childclasstwo inherits from fatherclass & nbsp;
<Asp: Label id = "lblchildclasstwofromfatherclass" runat = "server" text = "">
</ASP: Label>
<Br/>
<Br/>
Determine whether childclasstwo inherits from childclass & nbsp;
<Asp: Label id = "lblchildclasstwofromchildclass" runat = "server" text = "">
</ASP: Label>
<Br/>
<Br/>
Determine whether the myclass inherits from the fatherclass Class & nbsp;
<Asp: Label id = "lblmyclasstwofromfatherclass" runat = "server" text = "">
onclick = "Return button3_onclick () "/>
determine whether fatherclass implements the myinterface interface & nbsp;
</ASP: Label>
<Br/>
<Br/>
Determine whether childclass implements the myinterface interface & nbsp;
<Asp: Label id = "lblchildclassinterface" runat = "server" text = "">
</ASP: Label>
<Br/>
<Br/>
Determine whether childclasstwo has implemented the myinterface interface & nbsp;
<Asp: Label id = "lblchildclasstwointerface" runat = "server" text = "">
determine whether the myclass implements the myinterface interface & nbsp;
onclick =" Return button4_onclick () "/>
determine whether the fatherclass is an instance of the fatherclass & nbsp;
</ASP: Label>
<Br/>
<Br/>
Determine whether childclass is a fatherclass instance & nbsp;
<Asp: Label id = "lblmyclassclasstofatherclass" runat = "server" text = "">
</ASP: Label>
<Br/>
<Br/>
Judge whether myclass is an instance of the childclass Class & nbsp;
<Asp: Label id = "lblchildclasstochildclass" runat = "server" text = "">
</ASP: Label>
<Br/>
<Br/>
</Div>
</Form>
</Body>
</Html>
Result
In the future, reflection in Javascript is nothing more than the use of several methods,
So it is relatively simple, but it is not easy to understand the content.
2010-1-25