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<br /> image.height<br />

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代码:

This code refers to the button property of an object that is itself stored in the myform property of an object named document.

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:

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.

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()方法把对象转换为一个字符串,然后再试图把该字符串转换为一个数字。