简体   繁体   English

javascript中对象和关联数组之间有什么区别?

[英]What's the difference between objects and associated array in javascript?

The Confusing discussion 令人困惑的讨论

In this question , there is a discussion on the concepts of associated array and object in javaScript which I got a bit confused. 在这个问题中 ,我讨论了javaScript中相关数组和对象的概念,我有点困惑。

In this example code: 在此示例代码中:

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}>$/
    }
};

Here is the discussion makes me confused: 这是讨论让我困惑:

@steven.yang the outer object is not an associative array in your sample, but that is what is being asked for @ steven.yang外部对象不是样本中的关联数组,但这就是要求的内容

@sissonb what do you mean by 'outer object is not an associative array'? @sissonb你的意思是'外部对象不是一个关联数组'? I think associated array is expressed as object in javascript. 我认为关联数组在javascript中表示为对象。 The difference is in the notation - either through foo.bar or foo[bar] 不同之处在于符号 - 通过foo.barfoo[bar]

@steven.yang associated array means key => value. @ steven.yang关联数组意味着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. http://en.wikipedia.org/wiki/Associative_array你的内部对象有一个模式的键,包含这个关联数组的对象没有键。

My Understanding of Associated Array and Objects in JS 我对JS中关联数组和对象的理解

Associated array is defined as key-value pairs which is expressed as the object in JavaScript. 关联数组被定义为键值对,在JavaScript中表示为object

The outer object assigned to check has a key pattern and an value of another object. 分配给check的外部对象具有键pattern和另一个对象的值。 The inner object has keys of name , email ... and corresponding values of regular expression objects. 内部对象具有nameemail键......以及正则表达式对象的对应值。

Could both objects be counted as associative arrays? 两个对象都可以算作关联数组吗?

Not really, here's why: 不是真的,这就是原因:

var arr = new Array();
arr["foo"] = 100;
arr["bar"] = 200;
console.log(arr.length); // Prints 0.

Adding elements to an associative array should increase its length (IMO). 将元素添加到关联数组应该增加其长度(IMO)。

It looks and acts (somewhat) like an associative array because of syntactic sugar. 由于语法糖,它看起来和行为(有点)像一个关联数组。 What appear to be "array entries", however, are (just) object properties. 然而,似乎是“数组条目”的是(仅)对象属性。

If you define "associative array" as a data structure that stores information as a collection of key-value pairs, then yes, JavaScript objects are associative arrays. 如果将“关联数组”定义为将信息存储为键 - 值对集合的数据结构,则是,JavaScript对象是关联数组。

However, the phrase "associative array" is not generally used in the context of JavaScript, rather, we say " object ". 但是,短语“关联数组”通常不在JavaScript的上下文中使用,而是我们说“ 对象 ”。 I'd suggest sticking to standard JS terminology to avoid misunderstandings. 我建议坚持使用标准JS术语以避免误解。

Note that JS also has (non-associative) arrays , with elements accessed via numeric indexes. 请注意,JS还具有(非关联) 数组 ,其元素通过数字索引访问。 These are also objects and so allow non-numeric key properties, but this is generally considered bad practice. 也是对象,因此允许非数字键的属性,但是这通常被认为是不好的做法。

There are no associative-arrays in JavaScript. JavaScript中没有关联数组。 Everything is object. 一切都是对象。

Certainly they are similar but associative-arrays in JavaScript are just objects. 当然它们是相似的,但JavaScript中的关联数组只是对象。

Associative Array 关联数组

In computer science, an associative array (also called a map or a dictionary) is an abstract data type composed of a collection of (key,value) pairs, such that each possible key appears at most once in the collection. 在计算机科学中,关联数组(也称为地图或字典)是由(键,值)对的集合组成的抽象数据类型,使得每个可能的键在集合中最多出现一次。

As far as I know objects in JavaScript match that definition. 据我所知,JavaScript中的对象与该定义相匹配。

Of course there is no unique "Associative Array" object, that's any different then any other normal object. 当然,没有唯一的“关联数组”对象,这与任何其他普通对象有任何不同。 So if you want associative array functionality use a javascript object. 因此,如果您想要关联数组功能,请使用javascript对象。

However the following is a common piece of misinformation 然而,以下是一个常见的错误信息

There is no associative array in JavaScript JavaScript中没有关联数组

You should simply ignore these people, maybe try to convince them they are wrong. 你应该简单地忽略这些人,也许试着说服他们他们错了。

My response is coming late, but I hope this can help people to understand this difference. 我的回复迟到了,但我希望这可以帮助人们理解这种差异。 I was reading this post and there isn't a satisfactory definition of an associative array. 我正在阅读这篇文章,并没有令人满意的关联数组定义。 JavaScript doesn't have Associative Arrays. JavaScript没有关联数组。 These are just objects that you can treat as associative arrays for convenience. 这些只是为了方便您可以视为关联数组的对象。 In objects you store values as named properties, very similar to associative arrays in other programming languages. 在对象中,您将值存储为命名属性,与其他编程语言中的关联数组非常相似。 That's why you can access the properties like an associative array, but with methods associated to objects. 这就是为什么您可以像关联数组一样访问属性,但使用与对象关联的方法。 You can test creating an object and try all the things you can do with an associative array, but you can't do all the methods of an array. 您可以测试创建对象并尝试使用关联数组执行的所有操作,但不能执行数组的所有方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM