for (var i in Obj/array) {}

Source: Internet
Author: User

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

    1. var obj = {name: "Jack",

    2. Getname:function () {return this.name}

    3. };

    4. Output Name,getname

    5. For (var ATR in obj) {

    6. Alert (ATR);

    7. }

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

    1. var obj = {name: "Jack",

    2. Getname:function () {return this.name},

    3. Tostring:function () {return "I ' M Jack"}

    4. };

    5. For (var ATR in obj) {

    6. Alert (ATR);

    7. }

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) {}

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.