JS traversal page All object attributes and implementation methods _javascript skills

Source: Internet
Author: User
Tags reflection

JavaScript examples for for...in loops:

 
 

Today, the Java Tang blog on the web found a way to traverse the names and values of all the properties of a JavaScript object, so it is intuitive and convenient to use the method. The code is as follows:

* 
* Used to traverse the specified object all attribute names and values 
* obj needs to traverse the object 
* Author:jet Mah/ 
function Allprpos (obj) { 
//To save all attributes Name and value 
var props = ""; 
Start traversing 
for (var p in obj) { 
//method 
if (typeof (obj [p]) = = "function") { 
obj [P] (); 
} else { 
//P is the property name, Obj[p] is the value of the corresponding property 
props = + + "=" + obj [P] + "T"; 
} 
} 
Finally, all attribute 
alert (props) is displayed 
; 

The reflection mechanism of Ajax JavaScript, reflection mechanism refers to the program at runtime to obtain its own information. For example, an object can know at runtime what methods and attributes they have. Use for (... in ...) in JavaScript Statement to implement reflection with the following syntax:

For (var p in obj) { 
//statement 
}

In AJAX programming, it is often possible to dynamically change the style of an interface element, which can be changed by the object's style attribute, such as to change the background color to red, which can be written like this:

Element.style.backgroundcolor= "#ff0000";

Basically, the attributes in CSS are available in javascript:

function SetStyle (_style) { 
//Get to change the style of the interface object 
var element=getelement (); 
Element.style=_style; 
}

The entire style object is passed directly as a parameter in:

var style={ 
color: #ffffff, 
backgroundcolor: #ff0000, 
borderwidth:2px 
}

You can call the function this way:
SetStyle (style);

or write directly as:
SetStyle ({color: #ffffff, BackgroundColor: #ff0000, borderwidth:2px});

This code does not seem to have any problems, but in fact, when you use the parameter _style for Element.style assignment inside the SetStyle function, if the element already has a certain style, such as once executed:
Element.style.height= "20px";

The _style does not include the definition of height, so the height style of the element is lost, not the original result. To solve this problem, you can override the SetStyle function with a reflection mechanism:

function SetStyle (_style) { 
//Get to change the style of the interface object 
var element=getelement (); 
For (var p in _style) { 
element.style[p]=_style[p]; 
} 
}

The program iterates through each property of the _style, gets the property name, and then uses the bracket syntax to assign the corresponding property in the Element.style to the corresponding property in the _style.

The above JS traversal page all object properties and implementation method is small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

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.