逐句学英文-对象

Objectsˈɔbdʒikt对象

An object is a collection of named values.
对象是已命名的数据的集合。

These named values are usually referred to as properties of the object.
这些已命名的数据通常被作为对象的属性来引用。

For example, if an object named image has properties named width and height, you can refer to those properties like this:
例如,如果一个名为image的对象有一个名为width和一个名为height的属性,我们可以使用如下方式来引用这些属性:

image.width
image.height

Properties of objects are, in many ways, just like JavaScript variables; they can contain any type of data, including arrays, functions, and other objects.
对象的属性在很多方面都跟 JavaScript 的变量相似,属性可以是任何类型的数据,包括数组、函数和其他对象。

Thus, you might see JavaScript code like this:
所以,读者有可能会见到如下的 JavaScript代码:
document.myform.button

This code refers to the button property of an object that is itself stored in the myform property of an object named document.
这里引用了一个对象的button属性,而这个对象本身又存储在对象document的myform属性中。

When a function value is stored in a property of an object, that function is often called a method, and the property name becomes the method name.
如果一个函数值是存储在某个对象的属性中的,那么那个函数通常被称为方法,属性名也就变成了方法名。

Objects in JavaScript can serve as associative arrays; that is, they can associate arbitrary data values with arbitrary strings.
JavaScript 中的对象可以作为关联词组使用,因为它们能够将任意的数据值和任意的字符串关联起来。

Creating Objects 创建对象

Objects are created by invoking special constructor functions.
对象是通过调用特殊的构造函数创建的。

For example, the following lines all create new objects:
例如,下面的代码都创建了新对象:

var o = new Object();
var now = new Date()
var pattern = new RegExp("\\sjava\\s", "i")

Once you have created an object of your own, you can use and set its properties however you desire:
一旦创建了属于自己的对象,那么就可以根据自己的意愿设计并使用它的属性了:

var point = new Object();
point.x = 2.3;
point.y = -1.2;
Object Literals 对象直接量

An object literal consists of a comma-separated list of colon-separated property/value pairs, all enclosed within curly braces.
对象直接量是由一个包含在花括号之中的列表构成的,这个列表的元素是用冒号分隔的属性/值对,元素之间用逗号隔开。

Thus, the object point in the previous code can also be created and initialized with this line:
所以,可以使用如下的方式来创建并初始化上面代码中的point对象:

var point = { x:2.3, y:-1.2 };

Object literals can also be nested. For example:
对象直接量也可以嵌套。例如:

var rectangle = {
 
upperLeft: { x:2, y:2},
 
lowerRight: { x:4, y:4}
};

Finally, the property values used in object literals need not be constants; they can be arbitrary JavaScript expressions.
最后要说明的是,对象直接量中使用的属性值不必是常量,它可以是任意的 JavaScript 表达式。

Also, the property names in object literals may be strings rather than identifiers:
另外,对象直接量中的属性名可以是字符串而不是标识符:

var square = {
 
"upperLeft": { x:point.x, y:point.y},
 
'lowerRight': { x:(point.x + side), y:(point.y+side) }
};
Object Conversions 对象转换

When a non-null object is used in a Boolean context, it converts to true.
当一个非空对象用于布尔环境时,它转换为true

When an object is used in a string context, JavaScript calls the toString() method of the object and uses the string value retruned by that method.
当一个对象用于字符串环境,JavaScript 调用对象的 toString()方法,并且使用该函数返回的字符串的值。

When an object is used in a numeric context, JavaScript first calls the valueOf() method of the object. If this method returns a primitive value, that value is used.
当一个对象用于数字环境,JavaScript 首先调用该对象的valueOf()方法。如果这个方法返回一个基本类型的值,这个值会被使用。

In most cases, however, the valueOf() method returns the object itself. In this case, JavaScript first converts the object to a string with the toString() method and then attempts to convert the string to a number.
然而,在大多数情况下,valueOf()方法返回的是对象自己。在这种情况下,JavaScript 首先使用toString()方法把对象转换为一个字符串,然后再试图把该字符串转换为一个数字。

Referenceˈrefrəns参考

《JavaScript权威指南(影印版)(第5版)》 《JavaScript权威指南(第5版)》

崔凯博客
Powered by WordPress | Theme by ck web design | TOP 顶部
Copyright © 崔凯 All rights reserved. 蜀ICP备12025891号.