For .. in loop?

☆樱花仙子☆ 提交于 2019-12-23 04:28:05

问题


I'm losing it here.. I am now extremely confused about how this loop works.

From w3 schools:

var person={fname:"John",lname:"Doe",age:25}; 

for (x in person)
{
document.write(person[x] + " ");
}

person is an object with properties right? How are those properties being accessed with the brackets? I thought that was for arrays?

Why does this also work, and shouldn't it ONLY be like this?:

var person=[]; 
person["fname"] = "John";
person["lname"] = "Doe";
person["age"] = "25";


for (x in person)
{
document.write(person[x] + " ");
}

回答1:


There are two ways in which you have access to an object's properties:

  • obj.key
  • obj['key']

The advantage of the second method is that you can also provide the key dynamically, e.g. obj[x] in your example. obj.x would literally mean the x property (i.e. obj['x']), which is not what you want.

Arrays only work with brackets, but brackets are not limited to arrays. Arrays are under the hood also objects, but designed for numeric keys. You can still add properties with non-numeric keys to them, but that's not what they are designed for.




回答2:


You can access both Object literals as well as Arrays with the bracket operator in JavaScript. For Objects the bracket operator accesses a member of the Object by converting the value in the bracket to a String (if it is not a String) and checking if it is indeed a property (mdc).

Your second example suggests using an "associative Array" which is discouraged in JavaScript (link).

To answer your question: the standard way (imo) of writing a Map like structure -- i.e. an Object holding key-value-pairs -- to iterate over using a for-in loop is the Object literal; the standard way of writing a more traditional array is the Array Object.

var map = { "a": "string" , "b": 0 , "c": null } ;

    for(key in map) console.log("(!!) map # " + key + " : " + map[key] ) ;

var list = ["string",0,null] ;

    for(i = 0 ; i < list.length ; i++) console.log("(!!) list # " + i " : " + list[i] ) ;



回答3:


JavaScript objects properties can be accessed both with object[key] and object.key (and some other ways, most probably). Just the way they work.

Your second example, an array is a special object but still an object.




回答4:


http://bonsaiden.github.com/JavaScript-Garden/#object.general

"The properties of an object can be accessed in two ways, via either the dot notation, or the square bracket notation."




回答5:


in js objects are asociative arrays, meaning that they are nothing but a collection of key-value pairs.
If you are confused about the brakets, don't be! Javascript object properties can be accesed via the "." or the "[]" construction:

var a = {key : 'val'};
alert(a['key'] === a.key);

In most cases they work the same.
It's just a matter of preferences and browser implementation (eg : firefox works faster with brackets, while chrome works faster with the dots).
There are situations, where the dot construcion will fail: supose you have an object which has a property named "some-key".
If you want to acces it with the dot notation : object.some-key, you will most certain get an error, because the code is being interpreted as aa difference between 2 values : object.some - key. In this case you should use the brackets : object['some-key'].
There are other cases too, where the key contains a special character such as .,,,;,\,*...etc that already has a interpretation in javascript.



来源:https://stackoverflow.com/questions/6773981/for-in-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!