令人困惑的讨论
In this question ,有一个关于 javaScript 中关联数组和对象概念的讨论,我有点困惑。
在此示例代码中:
var check = {
pattern : {
name: /^[a-zA-Z-\s]{1,20}$/,
email: /^[a-zA-Z0-9._(-)]+@[a-zA-Z0-9.(-)]+\.[a-zA-Z]{1,4}$/,
pass: /.{6,40}/,
url: /^[(-)\w&:\/\.=\?,#+]{1,}$/,
aml: /<(.+)_([a-z]){1}>$/
}
};
这是让我感到困惑的讨论:
@steven.yang the outer object is not an associative array in your sample, but that is what is being asked for
@sissonb what do you mean by 'outer object is not an associative array'? I think associated array is expressed as object in javascript. The difference is in the notation - either through
foo.bar
orfoo[bar]
@steven.yang associated array means key => value. http://en.wikipedia.org/wiki/Associative_array Your inner object has a key of pattern, the object containing this associative array has no key.
我对JS中关联数组和对象的理解
关联数组是defined as键值对,在 JavaScript 中表示为 object
。
分配给 check
的外部对象有一个键 pattern
和另一个对象的值。内部对象有name
、email
等键和正则表达式对象对应的值。
这两个对象都可以算作关联数组吗?
最佳答案
不是真的,原因如下:
var arr = new Array();
arr["foo"] = 100;
arr["bar"] = 200;
console.log(arr.length); // Prints 0.
向关联数组添加元素应该增加它的长度(IMO)。
由于句法糖分,它看起来和行为(有点)像一个关联数组。然而,看似“数组条目”的内容(只是)对象属性。
关于javascript - javascript中的对象和关联数组有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8146104/