for (var i in Obj/array) {}
For...in defect: Performance issue, do not output obj built-in members, built-in member overrides, IE still does not display.
In a cross-browser design, we cannot rely on for-in to get the object's member name.
For...in is useful for debugging, displaying a list of names and values for all properties of an object.
The Navigator object contains the visitor's browser name, version, and more information
<script language= "JScript" >
var LANG = navigator.browserlanguage;
for (var i in navigator)
{
Alert (Navigator[i]);
document.write (i+ ":" +navigator[i]+ ", type:" +typeof (Navigator[i]) + "<br/>");
}
</script>
The results are as follows:
Appcodename:mozilla, type:string
Appname:microsoft Internet Explorer, type:string
appminorversion:0, type:string
Cpuclass:x86, type:string
Platform:win32, type:string
Plugins:, Type:object
Opsprofile:null, Type:object
Userprofile:null, Type:object
SYSTEMLANGUAGE:ZH-CN, type:string
USERLANGUAGE:ZH-CN, type:string
appversion:4.0 (compatible; MSIE 7.0; Windows NT 5.1;. NET CLR 2.0.50727;. NET CLR 1.1.4322;. NET CLR 3.0.04506.30;. NET CLR 3.0.04506.648), type:string
useragent:mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1;. NET CLR 2.0.50727;. NET CLR 1.1.4322;. NET CLR 3.0.04506.30;. NET CLR 3.0.04506.648), type:string
Online:true, Type:boolean
Cookieenabled:true, Type:boolean
Mimetypes:, Type:object
Use JavaScript to be careful with IE. This is clear to everyone, since needless to say. If a third-party code is referenced in JavaScript, or is used by someone else, for...in better not. Prototype also try to use less.
The for in statement is used to enumerate the properties (members) of an object, as follows
JS Code
var obj = {name: "Jack",
Getname:function () {return this.name}
};
Output Name,getname
For (var ATR in obj) {
Alert (ATR);
}
Did you notice that there are no built-in properties (or built-in members, hidden properties, and pre-defined attributes) that output obj's tostring,valueof. The for in is used to enumerate the display members (custom members) of the object.
If the built-in property is overridden, the following overrides the ToString of obj
JS Code
var obj = {name: "Jack",
Getname:function () {return this.name},
Tostring:function () {return "I ' M Jack"}
};
For (var ATR in obj) {
Alert (ATR);
}
What will it output?
1, IE6/7/8 and without rewriting ToString, output name,getname
2, FIREFOX3/CHROME2/OPERA9/SAFARI4 output name,getname,tostring
In conclusion: In a cross-browser design, we cannot rely on for-in to get the object's member name. Use, be cautious.
for (var i in Obj/array) {}