7.3 作为关联数组的对象(Objects as Associative Arrays)

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值