Home  >  Article  >  Web Front-end  >  Function to display all properties and methods of js object_javascript skills

Function to display all properties and methods of js object_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:44:211086browse

If you want to see the actual effect, you can first declare some properties and methods, otherwise you will not be able to see them. Look carefully below for examples.

Copy code The code is as follows:

function ShowObjProperty(Obj)
{
var PropertyList='';
var PropertyCount=0;
for(i in Obj){
if(Obj.i !=null)
PropertyList=PropertyList i 'Property:' Obj.i 'rn';
else
PropertyList=PropertyList i 'method rn';
}
alert(PropertyList);
}


Copy code The code is as follows:





Today I found a user on the Java Tang blog online A method to traverse all the property names and values ​​of a JavaScript object, which is very intuitive and convenient when you want to use the method. The code is as follows:
Copy code The code is as follows:

/*
* is used to traverse Specify all attribute names and values ​​of the object
* obj The object to be traversed
* author: Jet Mah
*/
function allPrpos ( obj ) {
// Used 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 attribute name, obj[p] is the value of the corresponding attribute
props = p " = " obj [ p ] " t " ;
}
}
// Finally display all properties
alert ( props ) ;
}

AJAX JavaScript Reflection mechanism, the reflection mechanism refers to the ability of the program to obtain its own information while running. For example, an object can know at runtime what methods and properties it has. Use the for(…in…) statement to implement reflection in JavaScript. The syntax is as follows:

for(var p in obj){
//Statement
}

in Ajax In programming, it is often necessary to dynamically change the style of interface elements. This can be changed through the style attribute of the object. For example, if you want to change the background color to red, you can write like this:
element.style.backgroundColor="#ff0000";

Basically all properties in CSS can be used in JavaScript:
Copy code The code is as follows:

function setStyle(_style){
//Get the interface object to change the style
var element=getElement();
element.style=_style;
}

Directly passed the entire style object as a parameter:
Copy the code The code is as follows:

var style={
color:#ffffff,
backgroundColor:#ff0000,
borderWidth:2px
}

This can be done at this time Call the function:
setStyle(style);

or write it directly:
setStyle({ color:#ffffff,backgroundColor:#ff0000,borderWidth:2px});

This code does not seem to have any problems, but in fact, when the parameter _style is used to assign a value to element.style inside the setStyle function, if the element already has a certain style, for example, it has been executed:
element.style. height="20px";

However, _style does not include the definition of height, so the height style of the element is lost, which is not the originally desired result. To solve this problem, you can use the reflection mechanism to rewrite the setStyle function:
Copy the code The code is as follows:

function setStyle(_style){
//Get the interface object to change the style
var element=getElement();
for(var p in _style){
element.style[p ]=_style[p];
}
}

The program traverses each attribute of _style to get the attribute name, and then uses square bracket syntax to assign the corresponding attribute in element.style to the value of the corresponding attribute in _style. Therefore, only the specified style is changed in the element, while other styles will not be changed, and the desired result is obtained. ^-^
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn