You've seen the . operator used to access the properties of an object. It is also possible to use the [] operator, which is more commonly used with arrays, to access these properties. Thus, the following two JavaScript expressions have the same value:
下面2行代码是等价的:
object.property // the property name is an identifier,
object["property"] //the property name is a string
The important difference to note between these two syntaxes is that in the first, the property name is an identifier, and in the second, the property name is a string. You'll see why this is so important shortly.
In C, C++, Java, and similar strongly typed languages, an object can have only a fixed number of properties, and the names of these properties must be defined in advance. Since JavaScript is a loosely typed language, this rule does not apply: a program can create any number of properties in any object. When you use the . operator to access a property of an object, however, the name of the property is expressed as an identifier. Identifiers must be typed literally into your JavaScript program; they are not a datatype, so they cannot be manipulated by the program.
(在C,C++.Java和其他类似的强类型语言中,一个对象只能拥有固定数目的属性,这些属性的名字必须是预先定义好的。JavaScript 是松散类型的原因,并不采用这条规则,在程序中可以为对象创建任意数目的属性,尽管如此,属性的名字是用标识符来表示的。表示符必须被逐字输入在你的javascript程序中;他们不是数据类型,所以不能在程序中被操纵)
On the other hand, when you access a property of an object with the [] array notation, the name of the property is expressed as a string. Strings are JavaScript datatypes, so they can be manipulated and created while a program is running.
(另一方面,当你用数组符号[]存取对象的属性的时候,属性的名字是用String来表示的,Strings 是Javascript的数据类型,所以他们可以被操纵并且可以在程序运行的时候被创建 )
So, for example, you can write the following code in JavaScript:
var addr = "";
for(i = 0; i < 4; i++)
{ addr += customer["address" + i] + '\n'; }
This code reads and concatenates the address0, address1, address2, and address3 properties of the customer object.
(这段代码读取了并且连接了 customer对象的 address0, address1, address2, 和address3 3个属性)
。。。。。。。。。。。。。。。。
When an object is used in this fashion, it is often called an associative arraya data structure that allows you to dynamically associate arbitrary values with arbitrary strings. The term map is often used to describe this situation as well: JavaScript objects map strings (property names) to values.
(如果一个对象采用这种方式,经常被称为关联数组,它是个数据结构,允许你动态的将任意数值和任意字符串关联在一起。。。)
The . notation for accessing properties makes JavaScript objects seem like the static objects of C++ and Java, and they work perfectly well in that capacity. But they also have the powerful ability to associate values with arbitrary strings. In this respect, JavaScript objects are much more like Perl hashes than C++ or Java objects.
(用符号.存取属性,使javascript对象看上去像 C++和java中的静态对象,它们同样很出色。但是他们具有更强大的能力将数值和任意字符串关联起来。从这个角度,javascript比C++和java对象更像Perl算法)
Chapter 6 introduced the for/in loop. The real power of this JavaScript statement becomes clear when you consider its use with associative arrays. To return to the stock portfolio example, you can use the following code after the user has entered her portfolio to compute its current total value:
var value = 0;
for (stock in portfolio) {
// For each stock in the portfolio, get the per share value
// and multiply it by the number of shares.
value += get_share_value(stock) * portfolio[stock];
}
This code cannot be written without the for/in loop because the names of the stocks aren't known in advance. This is the only way to extract those property names from the associative array (or JavaScript object) named portfolio.
分享到:
相关推荐
关联数组(associative array)是一种使用字符串或其他类型的值作为键的数组。与之相对的是索引数组(indexed array),其键是数字索引。在PHP中,数组的键通常是数字索引,但也可以是非数字的字符串索引,这样的...
当关联数组转换为JSON格式时,它将被表示为一个对象(在JSON中,对象由大括号`{}`包围,键值对以冒号分隔)。 在实际应用中,尤其是在为移动应用提供API接口时,通常会倾向于使用索引数组转成JSON,因为客户端(如...
- PHP支持两种类型的数组:索引数组(Indexed Arrays)和关联数组(Associative Arrays)。索引数组是通过数字索引来访问元素,而关联数组则是通过字符串键来访问元素。 - 数组中的每个元素由一个键(key)和一个...
PHP Arrays Single, Multi-dimensional, Associative and Object Arrays in PHP 7 英文无水印原版pdf pdf所有页面使用FoxitReader、PDF-XChangeViewer、SumatraPDF和Firefox测试都可以打开 本资源转载自网络,如...
集合主要包括数组(Arrays)和关联数组(Associative Arrays),其中数组又细分为索引数组(Indexed Tables)和可变数组(VARRAYs)。本文将深入探讨PL/SQL中的集合概念、类型以及它们在编程中的应用。 ### 索引...
在 awk 中数组叫做关联数组(associative arrays),因为下标记可以是数也可以是串。awk 中的数组不必提前声明,也不必声明大小。数组元素用 0 或空串来初始化,这根据上下文而定。例如: 一、定义方法 1:可以...
描述中的“how to provide string address in associative arrays in system verilog”进一步强调了这个主题,即如何在关联数组中使用字符串作为索引。 关联数组在SystemVerilog中定义如下: ```systemverilog ...
**JudyHash 开源关联数组** JudyHash 是一个开源的C++库,它提供了一种高效的数据结构,类似于STL中的映射(map)和集合(set)。这个库的独特之处在于它采用了一种特殊的算法设计,使得在处理大规模数据时能够保持...
容器类型同样包括关联数组或映射(Associative arrays/Maps),例如Java的HashMap、Python的dict和JavaScript的对象(通过键值对)。容器类型是编程中不可或缺的一部分,它们在组织数据方面起着至关重要的作用。 2....
echo "As an associative array:\n"; foreach ($Now as $Key => $Value) { echo "$Key => $Value\n"; } ?> ``` 运行这段代码,将会输出类似这样的结果: ``` sec => 1261918156 usec => 724964 minuteswest => 0 ...
这个压缩包的核心是WB,一个基于磁盘的排序关联数组(associative array)工具包。关联数组是一种数据结构,允许我们用键(key)来查找和操作值(value),类似于字典类型的数据结构在许多编程语言中的实现。 在C...
- **关联数组**(Associative Array):由字符串索引组成的数组。 - **多维数组**(Multidimensional Array):数组的元素也是数组。 **遍历**:访问数组中的每个元素的过程。 #### 二、索引数组遍历 **1. 使用 `...
Gain an in-depth understanding of PHP 7 arrays. After a quick overview of PHP 7, each chapter concentrates on single, multi-dimensional, associative, and object arrays. PHP Arrays is a first of its ...
- `$associative`:如果设置为`true`,JSON对象将被转换为关联数组,而不是`stdClass`对象。默认情况下,此参数为`false`,这意味着JSON对象将被转换为`stdClass`对象。 - `$depth`:指定递归深度,防止无限循环或过...
PHP支持两种类型的数组:索引数组(indexed arrays)和关联数组(associative arrays)。索引数组使用连续的整数作为键,而关联数组则允许使用任何字符串或数字作为键。在本例中,我们将关注如何将索引数组转换为...
键可以是整数或字符串,这使得PHP数组既可以用作索引数组(index array),也可以用作关联数组(associative array)。索引数组是指数组的键为整数的数组,通常用于按顺序存储数据;关联数组则是使用字符串作为键,...
对于数组,json_encode()会将索引数组(indexed array)转换为JSON数组格式,将关联数组(associative array)转换为JSON对象格式。例如: ```php $arr = array(111, 'aaa', 'bbb'); $arr1 = array('a'=>'aaa', 'b'...
联合数组是一种特殊的数组类型,它可以使用任意类型的元素作为索引,而不仅仅限于整数类型。在SystemVerilog中,联合数组可以使用关键字“assoc”来声明。例如: ```systemverilog typedef assoc int string int_...