Annotated ECMAScript 5.1

‟Ex igne vita”

15.2 Object Objects #

15.2.1 The Object Constructor Called as a Function #

When Object is called as a function rather than as a constructor, it performs a type conversion.

15.2.1.1 Object ( [ value ] ) #

When the Object function is called with no arguments or with one argument value, the following steps are taken:

  1. If value is null, undefined or not supplied, create and return a new Object object exactly as if the standard built-in Object constructor had been called with the same arguments (15.2.2.1).

  2. Return ToObject(value).

15.2.2 The Object Constructor #

When Object is called as part of a new expression, it is a constructor that may create an object.

15.2.2.1 new Object ( [ value ] ) #

When the Object constructor is called with no arguments or with one argument value, the following steps are taken:

  1. If value is supplied, then

    1. If Type(value) is Object, then

      1. If the value is a native ECMAScript object, do not create a new object but simply return value.

      2. If the value is a host object, then actions are taken and a result is returned in an implementation-dependent manner that may depend on the host object.

    2. If Type(value) is String, return ToObject(value).

    3. If Type(value) is Boolean, return ToObject(value).

    4. If Type(value) is Number, return ToObject(value).

  2. Asset: The argument value was not supplied or its type was Null or Undefined.

  3. Let obj be a newly created native ECMAScript object.

  4. Set the [[Prototype]] internal property of obj t to the standard built-in Object prototype object (15.2.4).

  5. Set the [[Class]] internal property of obj to "Object".

  6. Set the [[Extensible]] internal property of obj to true.

  7. Set the all the internal methods of obj as specified in 8.12

  8. Return obj.

15.2.3 Properties of the Object Constructor #

The value of the [[Prototype]] internal property of the Object constructor is the standard built-in Function prototype object.

Besides the internal properties and the length property (whose value is 1), the Object constructor has the following properties:

15.2.3.1 Object.prototype #

The initial value of Object.prototype is the standard built-in Object prototype object (15.2.4).

This property has the attributes {[[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

15.2.3.2 Object.getPrototypeOf ( O ) #

When the getPrototypeOf function is called with argument O, the following steps are taken:

  1. If Type(O) is not Object throw a TypeError exception.

  2. Return the value of the [[Prototype]] internal property of O.

15.2.3.3 Object.getOwnPropertyDescriptor ( O, P ) #

When the getOwnPropertyDescriptor function is called, the following steps are taken:

  1. If Type(O) is not Object throw a TypeError exception.

  2. Let name be ToString(P).

  3. Let desc be the result of calling the [[GetOwnProperty]] internal method of O with argument name.

  4. Return the result of calling FromPropertyDescriptor(desc) (8.10.4).

15.2.3.4 Object.getOwnPropertyNames ( O ) #

When the getOwnPropertyNames function is called, the following steps are taken:

  1. If Type(O) is not Object throw a TypeError exception.

  2. Let array be the result of creating a new object as if by the expression new Array() where Array is the standard built-in constructor with that name.

  3. Let n be 0.

  4. For each named own property P of O

    1. Let name be the String value that is the name of P.

    2. Call the [[DefineOwnProperty]] internal method of array with arguments ToString(n), the PropertyDescriptor {[[Value]]: name, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true}, and false.

    3. Increment n by 1.

  5. Return array.

NOTE If O is a String instance, the set of own properties processed in step 4 includes the implicit properties defined in 15.5.5.2 that correspond to character positions within the object’s [[PrimitiveValue]] String.

15.2.3.5 Object.create ( O [, Properties] ) #

The create function creates a new object with a specified prototype. When the create function is called, the following steps are taken:

  1. If Type(O) is not Object or Null throw a TypeError exception.

  2. Let obj be the result of creating a new object as if by the expression new Object() where Object is the standard built-in constructor with that name

  3. Set the [[Prototype]] internal property of obj to O.

  4. If the argument Properties is present and not undefined, add own properties to obj as if by calling the standard built-in function Object.defineProperties with arguments obj and Properties.

  5. Return obj.

15.2.3.6 Object.defineProperty ( O, P, Attributes ) #

The defineProperty function is used to add an own property and/or update the attributes of an existing own property of an object. When the defineProperty function is called, the following steps are taken:

  1. If Type(O) is not Object throw a TypeError exception.

  2. Let name be ToString(P).

  3. Let desc be the result of calling ToPropertyDescriptor with Attributes as the argument.

  4. Call the [[DefineOwnProperty]] internal method of O with arguments name, desc, and true.

  5. Return O.

15.2.3.7 Object.defineProperties ( O, Properties ) #

The defineProperties function is used to add own properties and/or update the attributes of existing own properties of an object. When the defineProperties function is called, the following steps are taken:

  1. If Type(O) is not Object throw a TypeError exception.

  2. Let props be ToObject(Properties).

  3. Let names be an internal list containing the names of each enumerable own property of props.

  4. Let descriptors be an empty internal List.

  5. For each element P of names in list order,

    1. Let descObj be the result of calling the [[Get]] internal method of props with P as the argument.

    2. Let desc be the result of calling ToPropertyDescriptor with descObj as the argument.

    3. Append desc to the end of descriptors.

  6. For each element desc of descriptors in list order,

    1. Call the [[DefineOwnProperty]] internal method of O with arguments P, desc, and true.

  7. Return O

If an implementation defines a specific order of enumeration for the for-in statement, that same enumeration order must be used to order the list elements in step 3 of this algorithm.

15.2.3.8 Object.seal ( O ) #

When the seal function is called, the following steps are taken:

  1. If Type(O) is not Object throw a TypeError exception.

  2. For each named own property name P of O,

    1. Let desc be the result of calling the [[GetOwnProperty]] internal method of O with P.

    2. If desc.[[Configurable]] is true, set desc.[[Configurable]] to false.

    3. Call the [[DefineOwnProperty]] internal method of O with P, desc, and true as arguments.

  3. Set the [[Extensible]] internal property of O to false.

  4. Return O.

15.2.3.9 Object.freeze ( O ) #

When the freeze function is called, the following steps are taken:

  1. If Type(O) is not Object throw a TypeError exception.

  2. For each named own property name P of O,

    1. Let desc be the result of calling the [[GetOwnProperty]] internal method of O with P.

    2. If IsDataDescriptor(desc) is true, then

      1. If desc.[[Writable]] is true, set desc.[[Writable]] to false.

    3. If desc.[[Configurable]] is true, set desc.[[Configurable]] to false.

    4. Call the [[DefineOwnProperty]] internal method of O with P, desc, and true as arguments.

  3. Set the [[Extensible]] internal property of O to false.

  4. Return O.

15.2.3.10 Object.preventExtensions ( O ) #

When the preventExtensions function is called, the following steps are taken:

  1. If Type(O) is not Object throw a TypeError exception.

  2. Set the [[Extensible]] internal property of O to false.

  3. Return O.

15.2.3.11 Object.isSealed ( O ) #

When the isSealed function is called with argument O, the following steps are taken:

  1. If Type(O) is not Object throw a TypeError exception.

  2. For each named own property name P of O,

    1. Let desc be the result of calling the [[GetOwnProperty]] internal method of O with P.

    2. If desc.[[Configurable]] is true, then return false.

  3. If the [[Extensible]] internal property of O is false, then return true.

  4. Otherwise, return false.

15.2.3.12 Object.isFrozen ( O ) #

When the isFrozen function is called with argument O, the following steps are taken:

  1. If Type(O) is not Object throw a TypeError exception.

  2. For each named own property name P of O,

    1. Let desc be the result of calling the [[GetOwnProperty]] internal method of O with P.

    2. If IsDataDescriptor(desc) is true then

      1. If desc.[[Writable]] is true, return false.

    3. If desc.[[Configurable]] is true, then return false.

  3. If the [[Extensible]] internal property of O is false, then return true.

  4. Otherwise, return false.

15.2.3.13 Object.isExtensible ( O ) #

When the isExtensible function is called with argument O, the following steps are taken:

  1. If Type(O) is not Object throw a TypeError exception.

  2. Return the Boolean value of the [[Extensible]] internal property of O.

15.2.3.14 Object.keys ( O ) #

When the keys function is called with argument O, the following steps are taken:

  1. If the Type(O) is not Object, throw a TypeError exception.

  2. Let n be the number of own enumerable properties of O

  3. Let array be the result of creating a new Object as if by the expression new Array(n) where Array is the standard built-in constructor with that name.

  4. Let index be 0.

  5. For each own enumerable property of O whose name String is P

    1. Call the [[DefineOwnProperty]] internal method of array with arguments ToString(index), the PropertyDescriptor {[[Value]]: P, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true}, and false.

    2. Increment index by 1.

  6. Return array.

If an implementation defines a specific order of enumeration for the for-in statement, that same enumeration order must be used in step 5 of this algorithm.

15.2.4 Properties of the Object Prototype Object #

The value of the [[Prototype]] internal property of the Object prototype object is null, the value of the [[Class]] internal property is "Object", and the initial value of the [[Extensible]] internal property is true.

15.2.4.1 Object.prototype.constructor #

The initial value of Object.prototype.constructor is the standard built-in Object constructor.

15.2.4.2 Object.prototype.toString ( ) #

When the toString method is called, the following steps are taken:

  1. If the this value is undefined, return "[object Undefined]".

  2. If the this value is null, return "[object Null]".

  3. Let O be the result of calling ToObject passing the this value as the argument.

  4. Let class be the value of the [[Class]] internal property of O.

  5. Return the String value that is the result of concatenating the three Strings "[object ", class, and "]".

15.2.4.3 Object.prototype.toLocaleString ( ) #

When the toLocaleString method is called, the following steps are taken:

  1. Let O be the result of calling ToObject passing the this value as the argument.

  2. Let toString be the result of calling the [[Get]] internal method of O passing "toString" as the argument.

  3. If IsCallable(toString) is false, throw a TypeError exception.

  4. Return the result of calling the [[Call]] internal method of toString passing O as the this value and no arguments.

NOTE 1 This function is provided to give all Objects a generic toLocaleString interface, even though not all may use it. Currently, Array, Number, and Date provide their own locale-sensitive toLocaleString methods.

NOTE 2 The first parameter to this function is likely to be used in a future version of this standard; it is recommended that implementations do not use this parameter position for anything else.

15.2.4.4 Object.prototype.valueOf ( ) #

When the valueOf method is called, the following steps are taken:

  1. Let O be the result of calling ToObject passing the this value as the argument.

  2. If O is the result of calling the Object constructor with a host object (15.2.2.1), then

    1. Return either O or another value such as the host object originally passed to the constructor. The specific result that is returned is implementation-defined.

  3. Return O.

15.2.4.5 Object.prototype.hasOwnProperty (V) #

When the hasOwnProperty method is called with argument V, the following steps are taken:

  1. Let P be ToString(V).

  2. Let O be the result of calling ToObject passing the this value as the argument.

  3. Let desc be the result of calling the [[GetOwnProperty]] internal method of O passing P as the argument.

  4. If desc is undefined, return false.

  5. Return true.

NOTE 1 Unlike [[HasProperty]] (8.12.6), this method does not consider objects in the prototype chain.

NOTE 2 The ordering of steps 1 and 2 is chosen to ensure that any exception that would have been thrown by step 1 in previous editions of this specification will continue to be thrown even if the this value is undefined or null.

15.2.4.6 Object.prototype.isPrototypeOf (V) #

When the isPrototypeOf method is called with argument V, the following steps are taken:

  1. If V is not an object, return false.

  2. Let O be the result of calling ToObject passing the this value as the argument.

  3. Repeat

    1. Let V be the value of the [[Prototype]] internal property of V.

    2. if V is null, return false

    3. If O and V refer to the same object, return true.

NOTE The ordering of steps 1 and 2 is chosen to preserve the behaviour specified by previous editions of this specification for the case where V is not an object and the this value is undefined or null.

15.2.4.7 Object.prototype.propertyIsEnumerable (V) #

When the propertyIsEnumerable method is called with argument V, the following steps are taken:

  1. Let P be ToString(V).

  2. Let O be the result of calling ToObject passing the this value as the argument.

  3. Let desc be the result of calling the [[GetOwnProperty]] internal method of O passing P as the argument.

  4. If desc is undefined, return false.

  5. Return the value of desc.[[Enumerable]].

NOTE 1 This method does not consider objects in the prototype chain.

NOTE 2 The ordering of steps 1 and 2 is chosen to ensure that any exception that would have been thrown by step 1 in previous editions of this specification will continue to be thrown even if the this value is undefined or null.

15.2.5 Properties of Object Instances #

Object instances have no special properties beyond those inherited from the Object prototype object.