对于..循环?
我在这里失去了它..我现在对这个循环是如何工作的非常困惑。
来自w3学校:
var person={fname:"John",lname:"Doe",age:25};
for (x in person)
{
document.write(person[x] + " ");
}
人是一个具有属性的对象,对吗?如何使用括号访问这些属性?我以为那是用于数组的?
为什么这也有效,而且不应该只是这样?:
var person=[];
person["fname"] = "John";
person["lname"] = "Doe";
person["age"] = "25";
for (x in person)
{
document.write(person[x] + " ");
}
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] + " ");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有两种方法可以访问对象的属性:
obj.key
obj['key']
第二种方法的优点是您还可以提供动态键,例如示例中的 obj[x] 。
obj.x
字面意思是x
属性(即obj['x']
),这不是您想要的。数组只能与括号一起使用,但括号不限于数组。数组本质上也是对象,但专为数字键而设计。您仍然可以向它们添加带有非数字键的属性,但这不是它们的设计目的。
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 thex
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.
您可以在 JavaScript 中使用括号运算符访问对象文字和数组。对于对象,括号运算符通过将括号中的值转换为字符串(如果它不是字符串)并检查它是否确实是属性来访问对象的成员(mdc)。
您的第二个示例建议使用 JavaScript 中不鼓励使用的“关联数组”(
回答你的问题:编写类似 Map 的结构(即保存键值对的对象)以使用 for-in 循环进行迭代的标准方法(imo)是对象文字;编写更传统数组的标准方法是
Array
Object
。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 theArray
Object
.JavaScript 对象属性可以通过 object[key] 和 object.key (以及其他一些最有可能的方式)来访问。就像他们的工作方式一样。
第二个例子,数组是一个特殊的对象,但仍然是一个对象。
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.
http://bonsaiden.github.com/JavaScript-Garden/#object.general
“可以通过两种方式访问对象的属性,通过点符号或方括号符号。”
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."
在 js 中,对象是关联数组,这意味着它们只不过是键值对的集合。
如果您对刹车感到困惑,请不要这样! Javascript 对象属性可以通过“.”访问。或“[]”结构:
在大多数情况下它们的工作原理相同。
这只是偏好和浏览器实现的问题(例如:firefox 使用括号工作得更快,而 chrome 使用点工作得更快)。
在某些情况下,点构造会失败:假设您有一个对象,该对象具有名为“some-key”的属性。
如果您想使用点符号访问它:
object.some-key
,您肯定会收到错误,因为代码被解释为两个值之间的差异:object。一些 - 键
。在这种情况下,您应该使用括号:object['some-key']
。还有其他情况,其中键包含特殊字符,例如
.
,,
,;
,\
,*
...等已经在 javascript 中有解释。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:
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.