Here is the term "reflection" is only a conceptual borrowing, and whether the characteristics of JavaScript mentioned below can be referred to as "reflection", JavaScript obtains the object type (via TypeOf operation), constructor (pass Observing constructor attributes) Even the characteristics of enumeration properties and methods have made us learn, research and use JavaScript to bring great convenience.
In JavaScript, you can use the for ... in method to enumerate all "Recommended" properties and methods in the object, including "inheritance" by the Prototype mechanism.
E.g:
Function classa ()
{
Classa.prototype.a = function () {return "a";
}
Function classb ()
{
Classb.Prototype.b = function () {Return "B";
} Classb.Prototype = new classa ();
Var msg = new array ();
VAR b = new classb ();
For (IDX IN B)
{
Msg.push (idx);
}
Alert (msg);
The above example lists all of the possible properties and methods of Classb's object B (the unmealed attribute methods include most JavaScript built-in objects, such as Object.ispropertyof, etc., but HTML objects and DOM objects are Specifications include A, B two member functions. In addition, since JavaScript is referenced by the associated array reference and the reference nouns is equivalent to the string "A" and B (ie, B ["A"] and Ba (), the cyclic body of the above example is changed to the MSG. Push (b [idx]); you can traverse all methods in B. and return the results to MSG.
Another interesting topic is about instanceof operations. JavaScript InstanceOf operation is strong enough, powerful to support "inheritance" discriminant, as the above example, the results of B Instance of Classa and B Instance of Classb are TRUE. This capability of instanceof operation is exactly what we need to use "Polymorphism".
Conversely, JavaScript TypeOf operation is a bit simple compared to powerful instanceof operations. It can only recognize the basic types of String, Number, and the Object and Function types. In this way, if you want to judge the type of object, TypeOf is in powerless. A more reluctant solution is to determine on the constructor property, but the constructor has a very annoying problem is that it returns the constructor in Prototype by default. Results The above examples of the object B have returned ClassA, rather than the expected classb, so, hand-modify the constructor property in each inheritance, in the example in the example in classb.prototype = new classa (); Add Classb.constructor = Classb.
Finally, talk about the problem of the DOM object. Dom objects and normal objects in JavaScript can distinguish between the TAGNAME properties, but this is not a very good way, because you can't help others define the tagname properties in the normal object. Another better way is that the constructor attribute of the DOM object is generally undefined (at the same time, obviously, instanceof object will return false, which is a significant feature of other objects, of course, premise is that JavaScript standard is not Will expand to the DOM object given the CONSTRUctor and strict inheritance mechanism! Common sense is often easier to make mistakes. End the topic, send a slightly packaged "reflection" management class, I hope it can bring some conveniences:
Function Reflector ()
{
Reflector.gettype = function (obj)
{
IF (obj == null) // null type
{
Return NULL;
}
Else if (Obj InstanceOf Object) // Ordinary object
{
Return Obj.constructor;
}
Else if (Obj.tagname! = null) // DOM object
{
Return Obj.tagname;
}
Else
{
Return TypeOf (OBJ);
}
}
Reflector.getattributes = function (obj)
{
Var methods = new arch;
For (IDX in Obj)
{
Methods.push (new type (obj [idx], classmanager.gettype (obj [idx)), IDX);
}
Return Methods;
}
Reflector.getattributenames = function (obj)
{
Var methods = new arch;
For (IDX in Obj)
{
Methods.push (IDX);
}
Return Methods;
}
}
// Describe the type of type, entity is an object entity, Type is an object type, name is an object name
Function Type (Entity, Type, Name)
{
THIS.ENTITY = Entity;
THIS.TYPE = TYPE;
THIS.NAME = Name;
}