Home>

javascript example of for ... in loop:

<html>
<head>
<title>A javascript example using a for ... in loop</title>
</head>
<body>
<script type="text/javascript">
//Create an object myobject and three attributes sitename, siteurl, sitecontent.
var myobject=new object ();
myobject.sitename="Bu La Bu La";
myobject.siteurl="blabla.cn";
myobject.sitecontent="Chinese site for web tutorial code gallery";
//Iterate through all the properties of the object
for (prop in myobject)
{
document.write ("property" "+ prop +" "is" + myobject [prop]);
document.write ("<br>");
}
</script>
</body>
</html>

Today, the java tang blog on the Internet found a method to traverse all the property names and values ​​of a certain object in JavaScript.It is very intuitive and convenient when i want to use the method.code show as below:

/*
* Used to traverse all the property names and values ​​of the specified object
* obj the object to be traversed
* author:jet mah
* /
function allprpos (obj) {
//used to save all property names and values
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";
}
}
//show all attributes at the end
alert (props);
}

reflection mechanism of ajax javascript,Reflection means that the program can obtain its own information when it is running.For example, an object can know which methods and properties it has at runtime. Use for (… in…) statements to implement reflection in javascript,Its syntax is as follows:

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

In ajax programming, it is often necessary to be able to dynamically change the style of interface elements.This can be changed by the object's style property,For example, to change the background color to red,Can be written like this:

element.style.backgroundcolor="#ff0000";

Basically, the properties owned by css can be used in javascript:

function setstyle (_style) {
//Get the interface object whose style i want to change
var element=getelement ();
element.style=_style;
}

Pass the entire style object directly as a parameter:

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

Then you can call the function like this:

setstyle (style);

Or write it directly:

setstyle ({color:#ffffff, backgroundcolor:#ff0000, borderwidth:2px));

This code looks fine.But in fact,When using the parameter _style to set element.style inside the setstyle function, if the element already has a certain style,For example:

element.style.height="20px";

However, the definition of height is not included in _style, so the height style of element is lost.Not the desired result at first.To solve this problem,You can override the setstyle function with reflection:

function setstyle (_style) {
//Get the interface object whose style i want to change
var element=getelement ();
for (var p in _style) {
element.style [p]=_ style [p];
}
}

Iterate through each attribute of _style in the program,Get the attribute name,Then use square bracket syntax to assign the corresponding attribute in element.style to the corresponding attribute in _style

  • Previous Simple example of js to implement thousands sign and retain a few decimal places
  • Next AngularJS basic ng-class-even instruction usage