Reflection in Javascript

Source: Internet
Author: User

Http://www.blogjava.net/flyingis/archive/2006/09/12/69213.html

Reflection and Application of JavaScript objects

Author: flyingis

Both Java and. Net have sound reflection mechanisms to process unknown objects and obtain their attributes and methods. Although JavaScript does not have a sound reflection system, it can still be passed during programming.CodeDesigned to implement basic functions similar to reflection.

Checks whether a JavaScript Object supports a specific property or method:

If ( Typeof (Obj. Property) ! =   " Undefined " ) {}

This statement is more effective than directly using "If (obj. property) "to describe more accurately, because when obj. when the value of property is false, 0, or null, the returned result is the opposite although the property exists.

If you want to check more details, you can use the instanceof operator to view the specific type of the attribute:

If (OBJ Instanceof Predefinedobj) {}

However, when condition detection is performed on the OBJ object, if the object types with multiple conditions have an inheritance relationship, pay attention to the code writing sequence, for example:

Function () Examinetype (OBJ) {
If (OBJ Instanceof Object) {
Alert ( " An object " );
Else   If (OBJ Instanceof Array) {
Alert ("An array");
}
}
}

The execution result of the above Code will assume that the original OBJ of the array type is an object, because the array itself is inherited from the object. Obviously, put array detection in front to get more accurate results. Therefore, to use instanceof to determine the object type, you must note that when two objects have an inheritance relationship, you should pay attention to the issue of detection sequence. Further we can think of it as follows, the object created in JSON is either an object or an array. It is of little significance to use instanceof to detect JSON objects.

Using JavaScript reflection, we can write a function to check whether the object has a function with a specific name, and then use this function for computation to implement the interface function in JavaScript, this lays the foundation for using design patterns in Ajax.

// This. getweight and this ["getweight"] have the same meaning
// Determines whether the object has a function with the specified name.
Object. Prototype. hasfunc =   Function (Func) {
Return This && This[Func]&& This[Func]InstanceofFunction;
}

Function Hasweight (OBJ) {
ReturnOBJ. hasfunc ("Getweight");
}

// Determines whether the parameter is of the numerical type.
Function Isnum (PARAM) {
ReturnParsefloat (PARAM)! =Nan;
}

// Calculate the weight of two objects
Function Calweight (obj1, obj2) {
VaR Total =   Null ;
If (Hasweight (obj1) && Hasweight (obj2 )) {
VaR W1 = Obj1.getweight ();
VaR W2 = Obj2.getweight ();
If (Isnum (W1) && Isnum (W2 )) {
Total=Parsefloat (W1)+Parsefloat (W2 );
}
}
Return Total;
}

Calweight first checks whether both objects have the getweight () function, then checks whether the getweight () calculation result is of the numerical type, and then adds the values to return the calculation result. Note that the parsefloat (PARAM) function can remove the non-numeric part of Param. If Param = 16 pm, parsefloat (16 pm) returns 16. If you do not use the parsefloat (PARAM) function to check the getweight () calculation result, it will bring security issues. In this case, you can set the object's getweight () designed to return strings or other types, we do not know the return type of the JavaScript function before calling it, because the JavaScript function does not have a pre-defined type.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.