As discussed earlier, all objects in JavaScript inherit from the Object class. javaScript中的所有对象都是从类Object中继承而来的。
While more specialized categories of objects, such as those created with the Date() and RegExp() constructors define properties and methods of their own, all objects, however created, also support the properties and methods defined by Object. Because of their universality, these properties and methods are of particular interest.
(尽管有一些专用类,比如由Date()和RegExp() 构造器创建的对象 可以由对象自己定义属性和方法。所有的对象 不管怎样被创建的,都支持由Object 定义的属性和方法,由于其通用性,。。。)
constructor 属性(The constructor Property)
In JavaScript, every object has a constructor property that refers to the constructor function that initializes the object。
For example, if I create an object d with the Date() constructor, the property d.constructor refers to Date:
var d = new Date();
d.constructor == Date; // Evaluates to true
d.constructor ==Object; //Evaluates to false;
var point = {x:1,y:2}
alert(point.constructor==Object); // Evaluates to true
constructor functions define new categories or classes of objects.
可以用以下2种方法来判断对象的类别:
1.if ((typeof o == "object") && (o.constructor == Date))
// Then do something with the Date object...
2.if ((typeof o == "object") && (o instanceof Date))
// Then do something with the Date object...
toString()方法 ( The toString() Method)
The toString() method takes no arguments; it returns a string that somehow represents the value of the object on which it is invoked. JavaScript invokes this method of an object whenever it needs to convert the object to a string. This occurs, for example, when you use the + operator to concatenate a string with an object or when you pass an object to a method such as alert() that expects a string.(翻译:toString()方法不带参数,它会返回一个字符串,这个字符串以某种方式代表了被引用对象的值)
The default toString() method is not very informative. For example, the following lines of code simply evaluate to the string "[object Object]":
var s = { x:1, y:1 }.toString(); // [object Object]
Because this default method does not display much useful information, many classes define their own versions of toString(). For example, when an array is converted to a string, you obtain a list of the array elements, themselves each converted to a string, and when a function is converted to a string, you obtain the source code for the function.
toLacaleString()方法 ( The toLacaleString() Method)
the Object class defines a toLocaleString() method in addition to its toString() method. The purpose of this method is to return a localized string representation of the object. The default toLocaleString() method defined by Object doesn't do any localization itself; it always returns exactly the same thing as toString(). Subclasses, however, may define their own versions of toLocaleString(). In ECMAScript v3, the Array, Date, and Number classes do define toLocaleString() methods that return localized values.
valueOf()方法 ( The valueOf() Method)
The hasOwnProperty() Method
The hasOwnProperty() method returns true if the object locally defines a noninherited property with the name specified by the single string argument. Otherwise, it returns false.(如果这个对象局部地定义了一个非继承的属性,并且属性名被指定为一个单个的String参数,hasOwnProperty()方法会返回true,)
var o = {};
o.hasOwnProperty("undef"); // false: the property is not defined
o.hasOwnProperty("toString"); // false: toString is an inherited property
Math.hasOwnProperty("cos"); // true: the Math object has a cos property
The propertyIsEnumerable() Method
The propertyIsEnumerable() method returns true if the object defines a noninherited property with the name specified by the single string argument to the method and if that property would be enumerated by a for/in loop. Otherwise, it returns false. For example:
var o = { x:1 };
o.propertyIsEnumerable("x"); // true: property exists and is enumerable
o.propertyIsEnumerable("y"); // false: property doesn't exist
o.propertyIsEnumerable("valueOf"); // false: property is inherited
The isPrototypeOf() Method
The isPrototypeOf() method returns true if the object to which the method is attached is the prototype object of the argument. Otherwise, it returns false.(如果isPrototypeOf方法所依附的对象(Object)是方法内(o)参数的原型,就会返回true,否则返回false)
For example:
var o = {}
Object.prototype.isPrototypeOf(o); // true: o.constructor == Object
Object.isPrototypeOf(o); // false
o.isPrototypeOf(Object.prototype); // false
Function.prototype.isPrototypeO(Object); //true:Object.constructor==Function
7.4. Universal Object Properties and Methods
通用对象属性和方法
7.4.1. The constructor Property
7.4.1. 构造属性
In JavaScript, every object has a constructor property that refers to the constructor function that initializes the object. For example, if I create an
object d with the Date() constructor, the property d.constructor refers to Date:
在JavaScript中,每个对象都有一个constructor属性指向初始化对象的构造方法,举个例子,如果创建对象d 和日期对象的构造,让属性“d.constructor” 指向 Date 对象
Since constructor functions define new categories or classes of objects, the constructor property can help determine the type of an object. For example, you
might use code like the following to determine the type of an unknown value:
对象的构造方法默认实例对象的类别或类型,构造属性能帮助确定对象的类型。举个例子,你可以像下边这样确定一个未知数据的类型
The instanceof operator checks the value of the constructor property, so the code above could also be written:
“instanceof ”操作符检查构造属性的值,所以这段代码也可以写成:
7.4.2. The toString() Method
7.4.2. toString()方法
The toString() method takes no arguments; it returns a string that somehow represents the value of the object on which it is invoked. JavaScript invokes this
method of an object whenever it needs to convert the object to a string. This occurs, for example, when you use the + operator to concatenate a string with
an object or when you pass an object to a method such as alert() that expects a string.
toString()方法没有参数;它返回一个字符串,其在某种程度上是对象被唤醒的值,JavaScript 唤醒一个对象的方法,同时它需要对象转换成字符串,这种情况,举个例子,当使
用“+”操作符链接一个字符串和对象,或当你通过一个对象的方法,如alert()预期的一个字符串。
The default toString() method is not very informative. For example, the following lines of code simply evaluate to the string "[object Object]":
默认toString() 方法不是非常详细的。举个例子,下面这个代码简单评定的字符串 "[object Object]"
Because this default method does not display much useful information, many classes define their own versions of toString(). For example, when an array is
converted to a string, you obtain a list of the array elements, themselves each converted to a string, and when a function is converted to a string, you
obtain the source code for the function.
因为这个默认的方法不显示过多信息,许多类默认了自己toString()这个方法的版本。举例,当一个数组转换成一个字符串,你获得的源码是整个方法。
Chapter 9 describes how to define a custom toString() method for your own object types.
第九章介绍怎样默认一个对象的toString() 方法为自己对象的类型。
7.4.3. The toLocaleString() Method
7.4.3. toLocaleString() 方法
In ECMAScript v3 and JavaScript 1.5, the Object class defines a toLocaleString() method in addition to its toString() method. The purpose of this method is
to return a localized string representation of the object. The default toLocaleString() method defined by Object doesn't do any localization itself; it
always returns exactly the same thing as toString(). Subclasses, however, may define their own versions of toLocaleString(). In ECMAScript v3, the Array,
Date, and Number classes do define toLocaleString() methods that return localized values.
在ECMAScript的第三版JavaScript 1.5版中,对象类默认一个toLocaleString() 方法,是在toString() 基础上附加的。这个方法的目的是返回一个本地化的字符串表示的对象。
默认toLocaleString() 方法定义的对象不能不能本地化自己;它总是返回一个与toString()方法子类返回的值相同。无论何时,toLocaleString()都可以默认自己的版本,在
ECMAScript 第三版Array, Date, Number 类默认的toLocaleString()方法返回本地化的值。
7.4.4. The valueOf() Method
The valueOf() method is much like the toString() method, but it is called when JavaScript needs to convert an object to some primitive type other than a
stringtypically, a number. JavaScript calls this method automatically if an object is used in a context where a primitive value is required. The default
valueOf() method does nothing interesting, but some of the built-in categories of objects define their own valueOf() method (see Date.valueOf(), for
example). Chapter 9 explains how to define a valueOf() method for custom object types you define.
7.4.4. valueOf()方法
valueOf()方法类似于toString() 方法,但是它会在 javascript 需要将对象转换成一个字符串或数字时调用。JavaScript 如果在对象的环境中自动调用这个方法,必须声明原始
值。默认对象的valueOf() 方法是空的,但一些内置类别的对象确定自己valueOf ( )方法,第九章定义 valueOf() 方法为custom对象类型。
7.4.5. The hasOwnProperty() Method
The hasOwnProperty() method returns true if the object locally defines a noninherited property with the name specified by the single string argument.
Otherwise, it returns false. For example:
7.4.5. hasOwnProperty() 方法
如果本地对象定义一个非遗传属性,属性名被指定到方法的一个字符串参数,hasOwnProperty()将返回true。否则返回false,例如:
7.4.6. The propertyIsEnumerable() Method
The propertyIsEnumerable() method returns true if the object defines a noninherited property with the name specified by the single string argument to the
method and if that property would be enumerated by a for/in loop. Otherwise, it returns false. For example:
7.4.6. propertyIsEnumerable() 方法
如果对象定义一个非遗传属性,属性名被指定到方法内的一个字符串参数,属性将循环到这个枚举的对象,那么propertyIsEnumerable()方法返回true,否则返回false,例如:
7.4.7. The isPrototypeOf() Method
The isPrototypeOf() method returns true if the object to which the method is attached is the prototype object of the argument. Otherwise, it returns false.
For example:
7.4.7. isPrototypeOf() 方法
无论对象附加的属性是哪个对象的参数,isPrototypeOf()方法都返回true,否则返回false,例如: