javascript中对象和关联数组有什么区别? [英] What's the difference between objects and associated array in javascript?

查看:31
本文介绍了javascript中对象和关联数组有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个问题中,有一个关于javaScript中关联数组和对象概念的讨论,我有点困惑.

In this question, there is a discussion on the concepts of associated array and object in javaScript which I got a bit confused.

在此示例代码中:

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 外部对象不是样本中的关联数组,但这正是我们所要求的

@steven.yang the outer object is not an associative array in your sample, but that is what is being asked for

@sissonb 外部对象不是关联数组"是什么意思?我认为关联数组在javascript中表示为对象.区别在于符号 - 通过 foo.barfoo[bar]

@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 or foo[bar]

@steven.yang 关联数组表示 key =>价值.http://en.wikipedia.org/wiki/Associative_array 你的内部对象有一个键模式,包含这个关联数组的对象没有键.

@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中关联数组和对象的理解

关联数组是定义为键值对,表示为JavaScript 中的对象.

分配给check 的外部对象有一个键pattern 和另一个对象的值.内部对象有nameemail ...的键和正则表达式对象的对应值.

The outer object assigned to check has a key pattern and an value of another object. The inner object has keys of name, email ... and corresponding values of regular expression objects.

两个对象都可以算作关联数组吗?

Could both objects be counted as associative arrays?

推荐答案

并非如此,原因如下:

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

向关联数组添加元素应增加其长度 (IMO).

Adding elements to an associative array should increase its length (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.

这篇关于javascript中对象和关联数组有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆