There are certain built-in objects available whenever an ECMAScript program begins execution. One, the global object, is in the scope chain of the executing program. Others are accessible as initial properties of the global object.
Unless specified otherwise, the [[Class]] property of a built-in object is "Function" if that built-in object has a [[Call]] property, or "Object" if that built-in object does not have a [[Call]] property.
Many built-in objects are functions: they can be invoked with arguments. Some of them furthermore are constructors: they are functions intended for use with the new operator. For each built-in function, this specification describes the arguments required by that function and properties of the Function object. For each built-in constructor, this specification furthermore describes properties of the prototype object of that constructor and properties of specific object instances returned by a new expression that invokes that constructor.
Unless otherwise specified in the description of a particular function, if a function or constructor described in this section is given fewer arguments than the function is specified to require, the function or constructor shall behave exactly as if it had been given sufficient additional arguments, each such argument being the undefined value.
Unless otherwise specified in the description of a particular function, if a function or constructor described in this section is given more arguments than the function is specified to allow, the behaviour of the function or constructor is undefined. In particular, an implementation is permitted (but not required) to throw a TypeError exception in this case.
NOTE
Implementations that add additional capabilities to the set of built-in
functions are encouraged to do so by adding new functions rather than
adding new parameters to existing functions.
Every built-in function and every built-in constructor has the Function prototype object, which is the initial value of the expression Function.prototype (15.3.2.1), as the value of its internal [[Prototype]] property.
Every built-in prototype object has the Object prototype object, which is the initial value of the expression Object.prototype (15.3.2.1), as the value of its internal [[Prototype]] property, except the Object prototype object itself.
None of the built-in functions described in this section shall implement the internal [[Construct]] method unless otherwise specified in the description of a particular function. None of the built-in functions described in this section shall initially have a prototype property unless otherwise specified in the description of a particular function. Every built-in Function object described in this section --- whether as a constructor, an ordinary function, or both --- has a length property whose value is an integer. Unless otherwise specified, this value is equal to the largest number of named arguments shown in the section headings for the function description, including optional parameters.
NOTE
For example, the Function object that is the initial value of the
slice property of the String prototype object is
described under the section heading "String.prototype.slice(start ,
end)" which shows the two named arguments start and end; therefore the
value of the length property of that Function
object is 2.
In every case, the length property of a built-in Function object described in this section has the attributes { ReadOnly, DontDelete, DontEnum } (and no others). Every other property described in this section has the attribute { DontEnum } (and no others) unless otherwise specified.
The global object does not have a [[Construct]] property; it is not possible to use the global object as a constructor with the new operator.
The global object does not have a [[Call]] property; it is not possible to invoke the global object as a function.
The values of the [[Prototype]] and [[Class]] properties of the global object are implementation-dependent.
The initial value of NaN is NaN (8.5). This property has the attributes { DontEnum, DontDelete}.
The initial value of Infinity is +∞ (8.5). This property has the attributes { DontEnum, DontDelete}.
The initial value of undefined is undefined (8.1). This property has the attributes { DontEnum, DontDelete}.
When the eval function is called with one argument x, the following steps are taken:
1. If x is not a string value, return x.
2. Parse x as a Program. If the parse fails, throw a SyntaxError exception (but see also clause 16).
3. Evaluate the program from step 2.
4. If Result(3). type is normal and its completion value is a value V, then return the value V.
5. If Result(3). type is normal and its completion value is empty, then return the value undefined.
6. Result(3). type must be throw. Throw Result(3). value as an exception.
If value of the eval property is used in any way other than a direct call (that is, other than by the explicit use of its name as an Identifier which is the MemberExpression in a CallExpression), or if the eval property is assigned to, an EvalError exception may be thrown.
The parseInt function produces an integer value dictated by interpretation of the contents of the string argument according to the specified radix. Leading whitespace in the string is ignored. If radix is undefined or 0, it is assumed to be 10 except when the number begins with the character pairs 0x or 0X, in which case a radix of 16 is assumed. Any radix-16 number may also optionally begin with the character pairs 0x or 0X.
When the parseInt function is called, the following steps are taken:
1. Call ToString(string).
2. Let S be a newly created substring of Result(1) consisting of the first character that is not a StrWhiteSpaceChar and all characters following that character. (In other words, remove leading white space.)
3. Let sign be 1.
4. If S is not empty and the first character of S is a minus sign -, let sign be -1.
5. If S is not empty and the first character of S is a plus sign + or a minus sign -, then remove the first character from S.
6. Let R = ToInt32(radix).
7. If R =0, go to step 11.
8. If R <2 orR > 36, then return NaN.
9. If R = 16, go to step 13.
10. Go to step 14.
11. Let R = 10.
12. If the length of S is at least 1 and the first character of S is "0", then at the implementation's discretion either let R = 8 or leave R unchanged.
13. If the length of S is at least 2 and the first two characters of S are either "0x" or "0X", then remove the first two characters from S and let R = 16.
14. If S contains any character that is not a radix- R digit, then let Z be the substring of S consisting of all characters before the first such character; otherwise, let Z be S.
15. If Z is empty, return NaN.
16. Compute the mathematical integer value that is represented by Z in radix- R notation, using the letters A-Z and a-z for digits with values 10 through 35. (However, if R is 10 and Z contains more than 20 significant digits, every significant digit after the 20th may be replaced by a 0 digit, at the option of the implementation; and if R is not 2, 4, 8, 10, 16, or 32, then Result(16) may be an implementation-dependent approximation to the mathematical integer value that is represented by Z in radix- R notation.)
17. Compute the number value for Result(16).
18. Return sign * Result(17).
NOTE
parseInt may interpret only a leading
portion of the string as an integer value; it ignores any characters
that cannot be interpreted as part of the notation of an integer, and
no indication is given that any such characters were ignored.
When radix is 0 or undefined and the string's number begins with a 0 digit not followed by an x or X, then the implementation may, at its discretion, interpret the number either as being octal or as being decimal. Implementations are encouraged to interpret numbers in this case as being decimal.
The parseFloat function produces a number value dictated by interpretation of the contents of the string argument as a decimal literal.
When the parseFloat function is called, the following steps are taken:
1. Call ToString(string).
2. Compute a substring of Result(1) consisting of the leftmost character that is not a StrWhiteSpaceChar and all characters to the right of that character.(In other words, remove leading white space.)
3. If neither Result(2) nor any prefix of Result(2) satisfies the syntax of a StrDecimalLiteral (see 0), return NaN.
4. Compute the longest prefix of Result(2), which might be Result(2) itself, which satisfies the syntax of a StrDecimalLiteral.
5. Return the number value for the MV of Result(4).
NOTE
parseFloat may interpret only
a leading portion of the string as a number value; it ignores any
characters that cannot be interpreted as part of the notation of an
decimal literal, and no indication is given that any such characters
were ignored.
Applies ToNumber to its argument, then returns true if the result is NaN, and otherwise returns false.
Applies ToNumber to its argument, then returns false if the result is NaN, +∞, or -∞, and otherwise returns true.
Uniform Resource Identifiers, or URIs, are strings that identify resources (e. g. web pages or files) and transport protocols by which to access them (e. g. HTTP or FTP) on the Internet. The ECMAScript language itself does not provide any support for using URIs except for functions that encode and decode URIs as described in 15.1.3.1, 15.1.3.2, 15.1.3.3 and 15.1.3.4.
NOTE
Many implementations of ECMAScript provide additional functions and
methods that manipulate web pages; these functions are beyond the scope
of this standard.
A URI is composed of a sequence of components separated by component separators. The general form is:
Scheme : First / Second ; Third ? Fourth
where the italicised names represent components and the ":", "/", ";" and"?" are reserved characters used as separators. The encodeURI and decodeURI functions are intended to work with complete URIs; they assume that any reserved characters in the URI are intended to have special meaning and so are not encoded. The encodeURIComponent and decodeURIComponent functions are intended to work with the individual component parts of a URI; they assume that any reserved characters represent text and so must be encoded so that they are not interpreted as reserved characters when the component is part of a complete URI.
The following lexical grammar specifies the form of encoded URIs. uri ::: uriCharactersopt
When a character to be included in a URI is not listed above or is not intended to have the special meaning sometimes given to the reserved characters, that character must be encoded. The character is first transformed into a sequence of octets using the UTF-8 transformation, with surrogate pairs first transformed from their UCS-2 to UCS-4 encodings. (Note that for code points in the range [0,127] this results in a single octet with the same value.) The resulting sequence of octets is then transformed into a string with each octet represented by an escape sequence of the form "% xx".
The encoding and escaping process is described by the hidden function Encode taking two string arguments string and unescapedSet. This function is defined for expository purpose only.
1. Compute the number of characters in string.
2. Let R be the empty string.
3. Let k be 0.
4. If k equals Result(1), return R.
5. Let C be the character at position k within string.
6. If C is not in unescapedSet, go to step 9.
7. Let S be a string containing only the character C.
8. Go to step 24.
9. If the code point value of C is not less than 0xDC00 and not greater than 0xDFFF, throw a URIError exception.
10. If the code point value of C is less than 0xD800 or greater than 0xDBFF, let V be the code point value of C and go to step 16.
11. Increase k by 1.
12. If k equals Result(1), throw a URIError exception.
13. Get the code point value of the character at position k within string.
14. If Result(13) is less than 0xDC00 or greater than 0xDFFF, throw a URIError exception.
15. Let V be (((the code point value of C) - 0xD800) * 0x400 + (Result(13) - 0xDC00) + 0x10000).
16. Let Octets be the array of octets resulting by applying the UTF-8 transformation to V, and let L be the array size.
17. Let j be 0.
18. Get the value at position j within Octets.
19. Let S be a string containing three characters "% XY" where XY are two uppercase hexadecimal digits encoding the value of Result(18).
20. Let R be a new string value computed by concatenating the previous value of R and S.
21. Increase j by 1.
22. If j is equal to L, go to step 25.
23. Go to step 18.
24. Let R be a new string value computed by concatenating the previous value of R and S.
25. Increase k by 1.
26. Go to step 4.
The unescaping and decoding process is described by the hidden function Decode taking two string arguments string and reservedSet. This function is defined for expository purpose only.
1. Compute the number of characters in string.
2. Let R be the empty string.
3. Let k be 0.
4. If k equals Result(1), return R.
5. Let C be the character at position k within string.
6. If C is not '% ', go to step 40.
7. Let start be k.
8. If k + 2 is greater than or equal to Result(1), throw a URIError exception.
9. If the characters at position (k+ 1) and (k + 2) within string do not represent hexadecimal digits, throw a URIError exception.
10. Let B be the 8-bit value represented by the two hexadecimal digits at position (k +1) and(k +2).
11. Increment k by 2.
12. If the most significant bit in B is 0, let C be the character with code point value B and go to step 37.
13. Let n be the smallest non-negative number such that (B << n) & 0x80 is equal to 0.
14. If n equals 1 or n is greater than 4, throw a URIError exception.
15. Let Octets be an array of 8-bit integers of size n.
16. Put B into Octets at position 0.
17. If k +(3 *(n - 1)) is greater than or equal to Result(1), throw a URIError exception.
18. Let j be 1.
19. If j equals n, go to step 29.
20. Increment k by 1.
21. If the character at position k is not '% ', throw a URIError exception.
22. If the characters at position (k +1) and (k + 2) within string do not represent hexadecimal digits, throw a URIError exception.
23. Let B be the 8-bit value represented by the two hexadecimal digits at position (k +1) and(k +2).
24. If the two most significant bits in B are not 10, throw a URIError exception.
25. Increment k by 2.
26. Put B into Octets at position j.
27. Increment j by 1.
28. Go to step 19.
29. Let V be the value obtained by applying the UTF-8 transformation to Octets, that is, from an array of octets into a 32-bit value.
30. If V is less than 0x10000, go to step 36.
31. If V is greater than 0x10FFFF, throw a URIError exception.
32. Let L be (((V - 0x10000) & 0x3FF) + 0xDC00).
33. Let H be ((((V - 0x10000) >> 10) & 0x3FF) + 0xD800).
34. Let S be the string containing the two characters with code point values H and L.
35. Go to step 41.
36. Let C be the character with code point value V.
37. If C is not in reservedSet, go to step 40.
38. Let S be the substring of string from position start to position k included.
39. Go to step 41.
40. Let S be the string containing only the character C.
41. Let R be a new string value computed by concatenating the previous value of R and S.
42. Increase k by 1.
43. Go to step 4.
NOTE 1
The syntax of Uniform Resource Identifiers is given in RFC2396.
NOTE 2
A formal description and implementation of UTF-8 is given in the
Unicode Standard, Version 2.0, Appendix A.
In UTF-8, characters are encoded using sequences of 1 to 6 octets. The only octet of a "sequence" of one has the higher-order bit set to 0, the remaining 7 bits being used to encode the character value. In a sequence of n octets, n> 1, the initial octet has the n higher-order bits set to 1, followed by a bit set to 0. The remaining bits of that octet contain bits from the value of the character to be encoded. The following octets all have the higher-order bit set to 1 and the following bit set to 0, leaving 6 bits in each to contain bits from the character to be encoded. The possible UTF-8 encodings of ECMAScript characters are:
| Code Point Value | Representation | 1st Octet | 2nd Octet | 3rd Octet | 4th Octet |
|---|---|---|---|---|---|
| 0x0000 - 0x007F | 00000000 0zzzzzzz | 0zzzzzzz | |||
| 0x0080 - 0x07FF | 00000yyy yyzzzzzz | 110yyyyy | 10zzzzzz | ||
| 0x0800 - 0xD7FF | xxxxyyyy yyzzzzzz | 1110xxxx | 10yyyyyy | 10zzzzzz | |
| 0xD800 - 0xDBFF
followed by 0xDC00 - 0xDFFF |
110110vv vvwwwwxx
followed by 110111yy yyzzzzzz |
11110uuu | 10uuwwww | 10xxyyyy | 10zzzzzz |
| 0xD800 -0xDBFF
not followed by 0xDC00 - 0xDFFF |
causes URIError | ||||
| 0xDC00 - 0xDFFF | causes URIError | ||||
| 0xE000 - 0xFFFF | xxxxyyyy yyzzzzzz | 1110xxxx | 10yyyyyy | 10zzzzzz |
Where
uuuuu = vvvv +1
to account for the addition of 0x10000 as in 3.7, Surrogates of the Unicode Standard version 2.0. The range of code point values 0xD800-0xDFFF is used to encode surrogate pairs; the above transformation combines a UCS-2 surrogate pair into a UCS-4 representation and encodes the resulting 21-bit value in UTF-8. Decoding reconstructs the surrogate pair.
The decodeURI function computes a new version of a URI in which each escape sequence and UTF-8 encoding of the sort that might be introduced by the encodeURI function is replaced with the character that it represents. Escape sequences that could not have been introduced by encodeURI are not replaced.
When the decodeURI function is called with one argument encodedURI, the following steps are taken:
1. Call ToString(encodedURI).
2. Let reservedURISet be a string containing one instance of each character valid in uriReserved plus "#".
3. Call Decode(Result(1), reservedURISet)
4. Return Result(3).
NOTE
The character "#" is not decoded from escape sequences even though it
is not a reserved URI character.
The decodeURIComponent function computes a new version of a URI in which each escape sequence and UTF-8 encoding of the sort that might be introduced by the encodeURIComponent function is replaced with the character that it represents.
When the decodeURIComponent function is called with one argument encodedURIComponent, the following steps are taken:
1. Call ToString(encodedURIComponent).
2. Let reservedURIComponentSet be the empty string.
3. Call Decode(Result(1), reservedURIComponentSet)
4. Return Result(3).
The encodeURI function computes a new version of a URI in which each instance of certain characters is replaced by one, two or three escape sequences representing the UTF-8 encoding of the character.
When the encodeURI function is called with one argument uri, the following steps are taken:
1. Call ToString(uri).
2. Let unescapedURISet be a string containing one instance of each character valid in uriReserved and uriUnescaped plus "#".
3. Call Encode(Result(1), unescapedURISet)
4. Return Result(3).
NOTE
The character "#" is not encoded to an escape sequence even though it
is not a reserved or unescaped URI character.
The encodeURIComponent function computes a new version of a URI in which each instance of certain characters is replaced by one, two or three escape sequences representing the UTF-8 encoding of the character.
When the encodeURIComponent function is called with one argument uriComponent, the following steps are taken:
1. Call ToString(uriComponent).
2. Let unescapedURIComponentSet be a string containing one instance of each character valid in uriUnescaped.
3. Call Encode(Result(1), unescapedURIComponentSet)
4. Return Result(3).
See 15.9.2.
See 15.11.6.1.
See 15.11.6.2.
See 15.11.6.3.
See 15.11.6.4.
See 15.11.6.5.
See 15.11.6.6.
See 15.8.
When Object is called as a function rather than as a constructor, it performs a type conversion.
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 if the object constructor had been called with the same arguments (15.2.2.1).
2. Return ToObject(value).
When Object is called as part of a new expression, it is a constructor that may create an object.
When the Object constructor is called with no arguments or with one argument value, the following steps are taken:
1. If value is not supplied, go to step 8.
2. If the type of value is not Object, go to step 5.
3. If the value is a native ECMAScript object, do not create a new object but simply return value.
4. 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.
5. If the type of value is String, return ToObject(value).
6. If the type of value is Boolean, return ToObject( value).
7. If the type of value is Number, return ToObject(value).
8. (The argument value was not supplied or its type was Null
or Undefined.)
Create a new native ECMAScript object.
The [[Prototype]] property of the newly constructed object is set to
the Object prototype object.
The [[Class]] property of the newly constructed object is set to
"Object".
The newly constructed object has no [[Value]] property.
Return the newly created native object.
The value of the internal [[Prototype]] property of the Object constructor is the Function prototype object.
Besides the internal properties and the length property (whose value is 1), the Object constructor has the following properties:
The initial value of Object.prototype is the Object prototype object (15.2.4).
This property has the attributes { DontEnum, DontDelete, ReadOnly }.
The value of the internal [[Prototype]] property of the Object prototype object is null and the value of the internal [[Class]] property is "Object".
The initial value of Object.prototype.constructor is the built-in Object constructor.
When the toString method is called, the following steps are taken:
1. Get the [[Class]] property of this object.
2. Compute a string value by concatenating the three strings "[object ", Result(1), and "]".
3. Return Result(2).
This function returns the result of calling
toString(). (which means the result of calling the function that is found by looking up the toString property in the this
object, which is not necessarily the implementation of toString in section 15.2.4.2)
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.
The valueOf method returns its this value. If the object is the result of calling the Object constructor with a host object (15.2.2.1), it is implementation-defined whether valueOf returns its this value or another value such as the host object originally passed to the constructor.
When the hasOwnProperty method is called with argument V, the following steps are taken:
1. Let O be this object.
2. Call ToString(V).
3. If O doesn't have a property with the name given by Result(2), return false.
4. Return true.
NOTE
Unlike [[HasProperty]] (8.6.2.4), this method
does not consider objects in the prototype chain.
When the isPrototypeOf method is called with argument V, the following steps are taken:
1. Let O be this object.
2. If V is not an object, return false.
3. Let V be the value of the [[Prototype]] property of V.
4. if V is null, return false
5. If O and V refer to the same object or if they refer to objects joined to each other (13.1.2), return true.
6. Go to step 3.
When the propertyIsEnumerable method is called with argument V, the following steps are taken:
1. Let O be this object.
2. Call ToString(V).
3. If O doesn't have a property with the name given by Result(2), return false.
4. If the property has the DontEnum attribute, return false.
5. Return true.
NOTE
This method does not consider objects in the prototype chain.
Object instances have no special properties beyond those inherited from the Object prototype object.
When Function is called as a function rather than as a constructor, it creates and initialises a new Function object. Thus the function call Function(...) is equivalent to the object creation expression new Function(...) with the same arguments.
When the Function function is called with some arguments p1, p2,..., pn, body (where n might be 0, that is, there are no "p" arguments, and where body might also not be provided), the following steps are taken:
1. Create and return a new Function object as if the function constructor had been called with the same arguments (15.3.2.1).
When Function is called as part of a new expression, it is a constructor: it initialises the newly created object.
The last argument specifies the body (executable code) of a function; any preceding arguments specify formal parameters.
When the Function constructor is called with some arguments p1, p2,..., pn, body (where n might be 0, that is, there are no "p" arguments, and where body might also not be provided), the following steps are taken:
1. Let P be the empty string.
2. If no arguments were given, let body bethe emptystringand go to step 13.
3. If one argument was given, let body be that argument and go to step 13.
4. Let Result(4) be the first argument.
5. Let P be ToString(Result(4)).
6. Let k be 2.
7. If k equals the number of arguments, let body be the k' th argument and go to step 13.
8. Let Result(8) be the k' th argument.
9. Call ToString(Result(8)).
10. Let P be the result of concatenating the previous value of P, the string "," (a comma), and Result(9).
11. Increase k by 1.
12. Go to step 7.
13. Call ToString(body).
14. If P is not parsable as a FormalParameterListopt then throw a SyntaxError exception.
15. If body is not parsable as FunctionBody then throw a SyntaxError exception.
16. Create a new Function object as specified in 13.2 with parameters specified by parsing P as a FormalParameterListopt and body specified by parsing body as a FunctionBody. Pass in a scope chain consisting of the global object as the Scope parameter.
17. Return Result(16).
A prototype property is automatically created for every function, to provide for the possibility that the function will be used as a constructor.
NOTE
It is permissible but not necessary to have one argument for each
formal parameter to be specified. For example, all three of the
following expressions produce the same result:
new Function("a", "b", "c", "return a+b+c")
new Function("a, b, c", "return a+b+c")
new Function("a,b", "c", "return a+b+c")
The value of the internal [[Prototype]] property of the Function constructor is the Function prototype object (15.3.4).
Besides the internal properties and the length property (whose value is 1), the Function constructor has the following properties:
The initial value of Function.prototype is the Function prototype object (15.3.4).
This property has the attributes { DontEnum, DontDelete, ReadOnly }.
The Function prototype object is itself a Function object (its [[Class]] is "Function") that, when invoked, accepts any arguments and returns undefined.
The value of the internal [[Prototype]] property of the Function prototype object is the Object prototype object (15.3.2.1).
It is a function with an "empty body"; if it is invoked, it merely returns undefined.
The Function prototype object does not have a valueOf property of its own; however, it inherits the valueOf property from the Object prototype Object.
The initial value of Function.prototype.constructor is the built-in Function constructor.
An implementation-dependent representation of the function is returned. This representation has the syntax of a FunctionDeclaration. Note in particular that the use and placement of white space, line terminators, and semicolons within the representation string is implementation-dependent.
The toString function is not generic; it throws a TypeError exception if its this value is not a Function object. Therefore, it cannot be transferred to other kinds of objects for use as a method.
The apply method takes two arguments, thisArg and argArray, and performs a function call using the [[Call]] property of the object. If the object does not have a [[Call]] property, a TypeError exception is thrown.
If thisArg is null or undefined, the called
function is passed the global object as the this value.
Otherwise, the called function is passed ToObject(thisArg) as
the this value.
If argArray is null or undefined, the called function is passed no arguments. Otherwise, if argArray is neither an array nor an arguments object (see 10.1.8), a TypeError exception is thrown. If argArray is either an array or an arguments object, the function is passed the (ToUint32(argArray. length)) arguments argArray[ 0], argArray[ 1], ..., argArray[ ToUint32(argArray. length)- 1].
The length property of the apply method is 2.
The call method takes one or more arguments, thisArg and (optionally) arg1, arg2 etc, and performs a function call using the [[Call]] property of the object. If the object does not have a [[Call]] property, a TypeError exception is thrown. The called function is passed arg1, arg2, etc. as the arguments.
If thisArg is null or undefined, the called
function is passed the global object as the this value.
Otherwise, the called function is passed ToObject(thisArg) as
the this value.
The length property of the call method is 1.
In addition to the required internal properties, every function instance has a [[Call]] property, a [[Construct]] property and a [[Scope]] property (see 8.6.2 and 13.2). The value of the [[Class]] property is "Function".
The value of the length property is usually an integer that indicates the "typical" number of arguments expected by the function. However, the language permits the function to be invoked with some other number of arguments. The behaviour of a function when invoked on a number of arguments other than the number specified by its length property depends on the function. This property has the attributes { DontDelete, ReadOnly, DontEnum }.
The value of the prototype property is used to initialise the internal [[Prototype]] property of a newly created object before the Function object is invoked as a constructor for that newly created object. This property has the attribute { DontDelete }.
Assume F is a Function object.
When the [[HasInstance]] method of F is called with value V, the following steps are taken:
1. If V is not an object, return false.
2. Call the [[Get]] method of F with property name "prototype".
3. Let O be Result(2).
4. If O is not an object, throw a TypeError exception.
5. Let V be the value of the [[Prototype]] property of V.
6. If V is null, return false.
7. If O and V refer to the same object or if they refer to objects joined to each other (13.1.2), return true.
8. Go to step 5.
Array objects give special treatment to a certain class of property names. A property name P (in the form of a string value) is an array index if and only if ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal to 232 - 1. Every Array object has a length property whose value is always a nonnegative integer less than 232 . The value of the length property is numerically greater than the name of every property whose name is an array index; whenever a property of an Array object is created or changed, other properties are adjusted as necessary to maintain this invariant. Specifically, whenever a property is added whose name is an array index, the length property is changed, if necessary, to be one more than the numeric value of that array index; and whenever the length property is changed, every property whose name is an array index whose value is not smaller than the new length is automatically deleted. This constraint applies only to properties of the Array object itself and is unaffected by length or array index properties that may be inherited from its prototype.
When Array is called as a function rather than as a constructor, it creates and initialises a new Array object. Thus the function call Array(...) is equivalent to the object creation expression new Array(...) with the same arguments.
When the Array function is called the following steps are taken:
1. Create and return a new Array object exactly as if the array constructor had been called with the same arguments (15.4.2).
When Array is called as part of a new expression, it is a constructor: it initialises the newly created object.
This description applies if and only if the Array constructor is given no arguments or at least two arguments.
The [[Prototype]] property of the newly constructed object is set to the original Array prototype object, the one that is the initial value of Array.prototype (15.4.3.1).
The [[Class]] property of the newly constructed object is set to "Array".
The length property of the newly constructed object is set to the number of arguments.
The 0 property of the newly constructed object is set to item0 (if supplied); the 1 property of the newly constructed object is set to item1 (if supplied); and, in general, for as many arguments as there are, the k property of the newly constructed object is set to argument k, where the first argument is considered to be argument number 0.
The [[Prototype]] property of the newly constructed object is set to the original Array prototype object, the one that is the initial value of Array.prototype (15.4.3.1). The [[Class]] property of the newly constructed object is set to "Array".
If the argument len is a Number and ToUint32(len) is equal to len, then the length property of the newly constructed object is set to ToUint32(len). If the argument len is a Number and ToUint32(len) is not equal to len, a RangeError exception is thrown.
If the argument len is not a Number, then the length property of the newly constructed object is set to 1 and the 0 property of the newly constructed object is set to len.
The value of the internal [[Prototype]] property of the Array constructor is the Function prototype object (15.3.4).
Besides the internal properties and the length property (whose value is 1), the Array constructor has the following properties:
The initial value of Array.prototype is the Array prototype object (15.4.4).
This property has the attributes { DontEnum, DontDelete, ReadOnly }.
The value of the internal [[Prototype]] property of the Array prototype object is the Object prototype object (15.2.3.1).
The Array prototype object is itself an array; its [[Class]] is "Array", and it has a length property (whose initial value is +0) and the special internal [[Put]] method described in 15.2.3.1.
In following descriptions of functions that are properties of the Array prototype object, the phrase "this object" refers to the object that is the this value for the invocation of the function. It is permitted for the this to be an object for which the value of the internal [[Class]] property is not "Array".
NOTE
The Array prototype object does not have a
valueOf property of its own; however, it
inherits the valueOf property from the
Object prototype Object.
The initial value of Array.prototype.constructor is the built-in Array constructor.
The result of calling this function is the same as if the built-in join method were invoked for this object with no argument.
The toString function is not generic; it throws a TypeError exception if its this value is not an Array object. Therefore, it cannot be transferred to other kinds of objects for use as a method.
The elements of the array are converted to strings using their toLocaleString methods, and these strings are then concatenated, separated by occurrences of a separator string that has been derived in an implementation-defined locale-specific way. The result of calling this function is intended to be analogous to the result of toString, except that the result of this function is intended to be locale-specific.
The result is calculated as follows:
1. Call the [[Get]] method of this object with argument "length".
2. Call ToUint32(Result(1)).
3. Let separator be the list-separator string appropriate for the host environment's current locale (this is derived in an implementation-defined way).
4. Call ToString(separator).
5. If Result(2) is zero, return the empty string.
6. Call the [[Get]] method of this object with argument "0".
7. If Result(6) is undefined or null, use the empty string; otherwise, call ToObject(Result(6)). toLocaleString().
8. Let R be Result(7).
9. Let k be 1.
10. If k equals Result(2), return R.
11. Let S be a string value produced by concatenating R and Result(4).
12. Call the [[Get]] method of this object with argument ToString(k).
13. If Result(12) is undefined or null, use the empty string; otherwise, call ToObject(Result(12)). toLocaleString().
14. Let R be a string value produced by concatenating S and Result(13).
15. Increase k by 1.
16. Go to step 10.
The toLocaleString function is not generic; it throws a TypeError exception if its this value is not an Array object. Therefore, it cannot be transferred to other kinds of objects for use as a method.
NOTE
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.
When the concat method is called with zero or more arguments item1, item2, etc., it returns an array containing the array elements of the object followed by the array elements of each argument in order.
The following steps are taken:
1. Let A be a new array created as if by the expression new Array().
2. Let n be 0.
3. Let E be this object.
4. If E is not an Array object, go to step 16.
5. Let k be 0.
6. Call the [[Get]] method of E with argument "length".
7. If k equals Result(6) go to step 19.
8. Call ToString(k).
9. If E has a property named by Result(8), go to step 10, but if E has no property named by Result(8), go to step 13.
10. Call ToString(n).
11. Call the [[Get]] method of E with argument Result(8).
12. Call the [[Put]] method of A with arguments Result(10) and Result(11).
13. Increase n by 1.
14. Increase k by 1.
15. Go to step 7.
16. Call ToString(n).
17. Call the [[Put]] method of A with arguments Result(16) and E.
18. Increase n by 1.
19. Get the next argument in the argument list; if there are no more arguments, go to step 22.
20. Let E be Result(19).
21. Go to step 4.
22. Call the [[Put]] method of A with arguments "length" and n.
23. Return A.
The length property of the concat method is 1.
NOTE
The concat function is intentionally
generic; it does not require that its this value
be an Array object. Therefore it can be transferred to other kinds of
objects for use as a method. Whether the
concat function can be applied successfully
to a host object is implementation-dependent.
The elements of the array are converted to strings, and these strings are then concatenated, separated by occurrences of the separator. If no separator is provided, a single comma is used as the separator.
The join method takes one argument, separator, and performs the following steps:
1. Call the [[Get]] method of this object with argument "length".
2. Call ToUint32(Result(1)).
3. If separator is undefined, let separator be the single-character string ",".
4. Call ToString(separator).
5. If Result(2) is zero, return the empty string.
6. Call the [[Get]] method of this object with argument "0".
7. If Result(6) is undefined or null, use the empty string; otherwise, call ToString(Result(6)).
8. Let R be Result(7).
9. Let k be 1.
10. If k equals Result(2), return R.
11. Let S be a string value produced by concatenating R and Result(4).
12. Call the [[Get]] method of this object with argument ToString(k).
13. If Result(12) is undefined or null, use the empty string; otherwise, call ToString(Result(12)).
14. Let R be a string value produced by concatenating S and Result(13).
15. Increase k by 1.
16. Go to step 10.
The length property of the join method is 1.
NOTE
The join function is intentionally
generic; it does not require that its this value
be an Array object. Therefore, it can be transferred to other kinds of
objects for use as a method. Whether the
join function can be applied successfully to
a host object is implementation-dependent.
The last element of the array is removed from the array and returned.
1. Call the [[Get]] method of this object with argument "length".
2. Call ToUint32(Result(1)).
3. If Result(2) is not zero, go to step 6.
4. Call the [[Put]] method of this object with arguments "length" and Result(2).
5. Return undefined.
6. Call ToString(Result(2)- 1).
7. Call the [[Get]] method of this object with argument Result(6).
8. Call the [[Delete]] method of this object with argument Result(6).
9. Call the [[Put]] method of this object with arguments "length" and (Result(2)- 1).
10. Return Result(7).
NOTE
The pop function is intentionally
generic; it does not require that its this value
be an Array object. Therefore it can be transferred to other kinds of
objects for use as a method. Whether the pop
function can be applied successfully to a host object is
implementation-dependent.
The arguments are appended to the end of the array, in the order in which they appear. The new length of the array is returned as the result of the call.
When the push method is called with zero or more arguments item1, item2, etc., the following steps are taken:
1. Call the [[Get]] method of this object with argument "length".
2. Let n be the result of calling ToUint32(Result(1)).
3. Get the next argument in the argument list; if there are no more arguments, go to step 7.
4. Call the [[Put]] method of this object with arguments ToString(n) and Result(3).
5. Increase n by 1.
6. Go to step 3.
7. Call the [[Put]] method of this object with arguments "length" and n.
8. Return n.
The length property of the push method is 1.
NOTE
The push function is intentionally
generic; it does not require that its this value
be an Array object. Therefore it can be transferred to other kinds of
objects for use as a method. Whether the
push function can be applied successfully to
a host object is implementation-dependent.
The elements of the array are rearranged so as to reverse their order. The object is returned as the result of the call.
1. Call the [[Get]] method of this object with argument "length".
2. Call ToUint32(Result(1)).
3. Compute floor(Result(2)/ 2).
4. Let k be 0.
5. If k equals Result(3), return this object.
6. Compute Result(2)-k-1.
7. Call ToString(k).
8. Call ToString(Result(6)).
9. Call the [[Get]] method of this object with argument Result(7).
10. Call the [[Get]] method of this object with argument Result(8).
11. If this object does not have a property named by Result(8), go to step 19.
12. If this object does not have a property named by Result(7), go to step 16.
13. Call the [[Put]] method of this object with arguments Result(7) and Result(10).
14. Call the [[Put]] method of this object with arguments Result(8) and Result(9).
15. Go to step 25.
16. Call the [[Put]] method of this object with arguments Result(7) and Result(10).
17. Call the [[Delete]] method on this object, providing Result(8) as the name of the property to delete.
18. Go to step 25.
19. If this object does not have a property named by Result(7), go to step 23.
20. Call the [[Delete]] method on this object, providing Result(7) as the name of the property to delete..
21. Call the [[Put]] method of this object with arguments Result(8) and Result(9).
22. Go to step 25.
23. Call the [[Delete]] method on this object, providing Result(7) as the name of the property to delete.
24. Call the [[Delete]] method on this object, providing Result(8) as the name of the property to delete.
25. Increase k by 1.
26. Go to step 5.
NOTE
The reverse function is intentionally
generic; it does not require that its this value
be an Array object. Therefore, it can be transferred to other kinds of
objects for use as a method. Whether the
reverse function can be applied successfully
to a host object is implementation-dependent.
The first element of the array is removed from the array and returned.
1. Call the [[Get]] method of this object with argument "length".
2. Call ToUint32(Result(1)).
3. If Result(2) is not zero, go to step 6.
4. Call the [[Put]] method of this object with arguments "length" and Result(2).
5. Return undefined.
6. Call the [[Get]] method of this object with argument 0.
7. Let k be 1.
8. If k equals Result(2), go to step 18.
9. Call ToString(k).
10. Call ToString(k- 1).
11. If this object has a property named by Result(9), go to step 12; but if this object has no property named by Result(9), then go to step 15.
12. Call the [[Get]] method of this object with argument Result(9).
13. Call the [[Put]] method of this object with arguments Result(10) and Result(12).
14. Go to step 16.
15. Call the [[Delete]] method of this object with argument Result(10).
16. Increase k by 1.
17. Go to step 8.
18. Call the [[Delete]] method of this object with argument ToString(Result(2)- 1).
19. Call the [[Put]] method of this object with arguments "length" and (Result(2)- 1).
20. Return Result(6).
NOTE
The shift function is intentionally
generic; it does not require that its this value
be an Array object. Therefore it can be transferred to other kinds of
objects for use as a method. Whether the
shift function can be applied successfully to
a host object is implementation-dependent.
The slice method takes two arguments, start and end, and returns an array containing the elements of the array from element start up to, but not including, element end (or through the end of the array if end is undefined). If start is negative, it is treated as (length+ start) where length is the length of the array. If end is negative, it is treated as (length+ end) where length is the length of the array. The following steps are taken:
1. Let A be a new array created as if by the expression new Array().
2. Call the [[Get]] method of this object with argument "length".
3. Call ToUint32(Result(2)).
4. Call ToInteger(start).
5. If Result(4) is negative, use max((Result(3)+ Result(4)), 0); else use min(Result(4), Result(3)).
6. Let k be Result(5).
7. If end is undefined, use Result(3); else use ToInteger(end).
8. If Result(7) is negative, use max((Result(3)+ Result(7)), 0); else use min(Result(7), Result(3)).
9. Let n be 0.
10. If k is greater than or equal to Result(8), go to step 19.
11. Call ToString(k).
12. If this object has a property named by Result(11), go to step 13; but if this object has no property named by Result(11), then go to step 16.
13. Call ToString(n).
14. Call the [[Get]] method of this object with argument Result(11).
15. Call the [[Put]] method of A with arguments Result(13) and Result(14).
16. Increase k by 1.
17. Increase n by 1.
18. Go to step 10.
19. Call the [[Put]] method of A with arguments "length" and n.
20. Return A.
The length property of the slice method is 2.
NOTE
The slice function is intentionally
generic; it does not require that its this value
be an Array object. Therefore it can be transferred to other kinds of
objects for use as a method. Whether the
slice function can be applied successfully to
a host object is implementation-dependent.
The elements of this array are sorted. The sort is not necessarily stable (that is, elements that compare equal do not necessarily remain in their original order). If comparefn is not undefined, it should be a function that accepts two arguments x and y and returns a negative value if x < y, zero if x = y, or a positive value if x > y.
If comparefn is not undefined and is not a consistent comparison function for the elements of this array (see below), the behaviour of sort is implementation-defined. Let len be ToUint32(this.length). If there exist integers i and j and an object P such that all of the conditions below are satisfied then the behaviour of sort is implementation-defined:
Otherwise the following steps are taken.
1. Call the [[Get]] method of this object with argument "length".
2. Call ToUint32(Result(1)).
3. Perform an implementation-dependent sequence of calls to the [[Get]] , [[Put]], and [[Delete]] methods of this object and to SortCompare (described below), where the first argument for each call to [[Get]], [[Put]], or [[Delete]] is a nonnegative integer less than Result(2) and where the arguments for calls to SortCompare are results of previous calls to the [[Get]] method.
4. Return this object.
The returned object must have the following two properties.
Here the notation old[j] is used to refer to the hypothetical result of calling the [[Get]] method of this object with argument j before this function is executed, and the notation new[j] to refer to the hypothetical result of calling the [[Get]] method of this object with argument j after this function has been executed.
A function comparefn is a consistent comparison function for a set of values S if all of the requirements below are met for all values a, b, and c (possibly the same value) in the set S: The notation a <CF b means comparefn(a, b)< 0; a =CF b means comparefn(a, b) = 0 (of either sign); and a >CF b means comparefn(a, b)> 0.
NOTE
The above conditions are necessary and sufficient to ensure that
comparefn divides the set S into equivalence classes and that these
equivalence classes are totally ordered.
When the SortCompare operator is called with two arguments j and k, the following steps are taken:
1. Call ToString(j).
2. Call ToString(k).
3. If this object does not have a property named by Result(1), and this object does not have a property named by Result(2), return +0.
4. If this object does not have a property named by Result(1), return 1.
5. If this object does not have a property named by Result(2), return -1.
6. Call the [[Get]] method of this object with argument Result(1).
7. Call the [[Get]] method of this object with argument Result(2).
8. Let x be Result(6).
9. Let y be Result(7).
10. If x and y are both undefined, return +0.
11. If x is undefined, return 1.
12. If y is undefined, return -1.
13. If the argument comparefn is undefined, go to step 16.
14. Call comparefn with arguments x and y.
15. Return Result(14).
16. Call ToString(x).
17. Call ToString(y).
18. If Result(16) < Result(17), return -1.
19. If Result(16) > Result(17), return 1.
20. Return +0.
NOTE 1
Because non-existent property values always compare greater than
undefined property values, and
undefined always compares greater than any other
value, undefined property values always sort to the end of the result,
followed by non-existent property values.
NOTE 2
The sort function is intentionally
generic; it does not require that its this value
be an Array object. Therefore, it can be transferred to other kinds of
objects for use as a method. Whether the
sort function can be applied successfully to
a host object is implementation-dependent.
When the splice method is called with two or more arguments start, deleteCount and (optionally) item1, item2, etc., the deleteCount elements of the array starting at array index start are replaced by the arguments item1, item2, etc. The following steps are taken:
1. Let A be a new array created as if by the expression new Array().
2. Call the [[Get]] method of this object with argument "length".
3. Call ToUint32(Result(2)).
4. Call ToInteger(start).
5. If Result(4) is negative, use max((Result(3)+ Result(4)), 0); else use min(Result(4), Result(3)).
6. Compute min(max(ToInteger(deleteCount), 0), Result(3)- Result(5)).
7. Let k be 0.
8. If k equals Result(6), go to step 16.
9. Call ToString(Result(5)+ k).
10. If this object has a property named by Result(9), go to step 11; but if this object has no property named by Result(9), then go to step 14.
11. Call ToString(k).
12. Call the [[Get]] method of this object with argument Result(9).
13. Call the [[Put]] method of A with arguments Result(11) and Result(12).
14. Increment k by 1.
15. Go to step 8.
16. Call the [[Put]] method of A with arguments "length" and Result(6).
17. Compute the number of additional arguments item1, item2, etc.
18. If Result(17) is equal to Result(6), go to step 48.
19. If Result(17) is greater than Result(6), go to step 37.
20. Let k be Result(5).
21. If k is equal to (Result(3)- Result(6)), go to step 31.
22. Call ToString(k+ Result(6)).
23. Call ToString(k+ Result(17)).
24. If this object has a property named by Result(22), go to step 25; but if this object has no property named by Result(22), then go to step 28.
25. Call the [[Get]] method of this object with argument Result(22).
26. Call the [[Put]] method of this object with arguments Result(23) and Result(25).
27. Go to step 29.
28. Call the [[Delete]] method of this object with argument Result(23).
29. Increase k by 1.
30. Go to step 21.
31. Let k be Result(3).
32. If k is equal to (Result(3)- Result(6)+ Result(17)), go to step 48.
33. Call ToString(k- 1).
34. Call the [[Delete]] method of this object with argument Result(33).
35. Decrease k by 1.
36. Go to step 32.
37. Let k be (Result(3)- Result(6)).
38. If k is equal to Result(5), go to step 48.
39. Call ToString(k+ Result(6)- 1).
40. Call ToString(k+ Result(17)- 1)
41. If this object has a property named by Result(39), go to step 42; but if this object has no property named by Result(39), then go to step 45.
42. Call the [[Get]] method of this object with argument Result(39).
43. Call the [[Put]] method of this object with arguments Result(40) and Result(42).
44. Go to step 46.
45. Call the [[Delete]] method of this object with argument Result(40).
46. Decrease k by 1.
47. Go to step 38.
48. Let k be Result(5).
49. Get the next argument in the part of the argument list that starts with item1; if there are no more arguments, go to step 53.
50. Call the [[Put]] method of this object with arguments ToString( k) and Result(49).
51. Increase k by 1.
52. Go to step 49.
53. Call the [[Put]] method of this object with arguments "length" and (Result(3)- Result(6)+ Result(17)).
54. Return A.
The length property of the splice method is 2.
NOTE
The splice function is intentionally
generic; it does not require that its this value
be an Array object. Therefore it can be transferred to other kinds of
objects for use as a method. Whether the
splice function can be applied successfully
to a host object is implementation-dependent.
The arguments are prepended to the start of the array, such that their order within the array is the same as the order in which they appear in the argument list.
When the unshift method is called with zero or more arguments item1, item2, etc., the following steps are taken:
1. Call the [[Get]] method of this object with argument "length".
2. Call ToUint32(Result(1)).
3. Compute the number of arguments.
4. Let k be Result(2).
5. If k is zero, go to step 15.
6. Call ToString(k- 1).
7. Call ToString(k+ Result(3)- 1).
8. If this object has a property named by Result(6), go to step 9; but if this object has no property named by Result(6), then go to step 12.
9. Call the [[Get]] method of this object with argument Result(6).
10. Call the [[Put]] method of this object with arguments Result(7) and Result(9).
11. Go to step 13.
12. Call the [[Delete]] method of this object with argument Result(7).
13. Decrease k by 1.
14. Go to step 5.
15. Let k be 0.
16. Get the next argument in the part of the argument list that starts with item1; if there are no more arguments, go to step 21.
17. Call ToString(k).
18. Call the [[Put]] method of this object with arguments Result(17) and Result(16).
19. Increase k by 1.
20. Go to step 16.
21. Call the [[Put]] method of this object with arguments "length" and (Result(2)+ Result(3)).
22. Return (Result(2)+ Result(3)).
The length property of the unshift method is 1.
NOTE
The unshift function is intentionally
generic; it does not require that its this value
be an Array object. Therefore it can be transferred to other kinds of
objects for use as a method. Whether the
unshift function can be applied successfully
to a host object is implementation-dependent.
Array instances inherit properties from the Array prototype object and also have the following properties.
Array objects use a variation of the [[Put]] method used for other native ECMAScript objects (8.6.2.2).
Assume A is an Array object and P is a string. When the [[Put]] method of A is called with property P and value V, the following steps are taken:
1. Call the [[CanPut]] method of A with name P.
2. If Result(1) is false, return.
3. If A doesn't have a property with name P, go to step 7.
4. If P is "length", go to step 12.
5. Set the value of property P of A to V.
6. Go to step 8.
7. Create a property with name P, set its value to V and give it empty attributes.
8. If P is not an array index, return.
9. If ToUint32(P) is less than the value of the length property of A, then return.
10. Change (or set) the value of the length property of A to ToUint32(P)+ 1.
11. Return.
12. Compute ToUint32(V).
13. If Result(12) is not equal to ToNumber(V), throw a RangeError exception.
14. For every integer k that is less than the value of the length property of A but not less than Result(12), if A itself has a property (not an inherited property) named ToString(k), then delete that property.
15. Set the value of property P of A to Result(12).
16. Return.
The length property of this Array object is always numerically greater than the name of every property whose name is an array index.
The length property has the attributes { DontEnum, DontDelete }.
When String is called as a function rather than as a constructor, it performs a type conversion.
Returns a string value (not a String object) computed by ToString(value). If value is not supplied, the empty string "" is returned.
When String is called as part of a new expression, it is a constructor: it initialises the newly created object.
The [[Prototype]] property of the newly constructed object is set to the original String prototype object, the one that is the initial value of String.prototype (15.5.3.1).
The [[Class]] property of the newly constructed object is set to "String".
The [[Value]] property of the newly constructed object is set to ToString(value), or to the empty string if value is not supplied.
The value of the internal [[Prototype]] property of the String constructor is the Function prototype object (15.3.4).
Besides the internal properties and the length property (whose value is 1), the String constructor has the following properties:
The initial value of String.prototype is the String prototype object (15.5.4).
This property has the attributes { DontEnum, DontDelete, ReadOnly }.
Returns a string value containing as many characters as the number of arguments. Each argument specifies one character of the resulting string, with the first argument specifying the first character, and so on, from left to right. An argument is converted to a character by applying the operation ToUint16 (9.7) and regarding the resulting 16-bit integer as the code point value of a character. If no arguments are supplied, the result is the empty string.
The length property of the fromCharCode function is 1.
The String prototype object is itself a String object (its [[Class]] is "String") whose value is an empty string.
The value of the internal [[Prototype]] property of the String prototype object is the Object prototype object (15.2.3.1).
The initial value of String.prototype.constructor is the built-in String constructor.
Returns this string value. (Note that, for a String object, the toString method happens to return the same thing as the valueOf method.)
The toString function is not generic; it throws a TypeError exception if its this value is not a String object. Therefore, it cannot be transferred to other kinds of objects for use as a method.
Returns this string value.
The valueOf function is not generic; it throws a TypeError exception if its this value is not a String object. Therefore, it cannot be transferred to other kinds of objects for use as a method.
Returns a string containing the character at position pos in the string resulting from converting this object to a string. If there is no character at that position, the result is the empty string. The result is a string value, not a String object.
If pos is a value of Number type that is an integer, then the result of x.charAt( pos) is equal to the result of x.substring( pos, pos+ 1).
When the charAt method is called with one argument pos, the following steps are taken:
1. Call ToString, giving it the this value as its argument.
2. Call ToInteger(pos).
3. Compute the number of characters in Result(1).
4. If Result(2) is less than 0 or is not less than Result(3), return the empty string.
5. Return a string of length 1, containing one character from Result(1), namely the character at position Result(2), where the first (leftmost) character in Result(1) is considered to be at position 0, the next one at position 1, and so on.
NOTE
The charAt function is intentionally
generic; it does not require that its this value
be a String object. Therefore, it can be transferred to other kinds of
objects for use as a method.
Returns a number (a nonnegative integer less than 216 ) representing the code point value of the character at position pos in the string resulting from converting this object to a string. If there is no character at that position, the result is NaN.
When the charCodeAt method is called with one argument pos, the following steps are taken:
1. Call ToString, giving it the this value as its argument.
2. Call ToInteger(pos).
3. Compute the number of characters in Result(1).
4. If Result(2) is less than 0 or is not less than Result(3), return NaN.
5. Return a value of Number type, whose value is the code point value of the character at position Result(2) in the string Result(1), where the first (leftmost) character in Result(1) is considered to be at position 0, the next one at position 1, and so on.
NOTE
The charCodeAt function is intentionally
generic; it does not require that its this value
be a String object. Therefore it can be transferred to other kinds of
objects for use as a method.
When the concat method is called with zero or more arguments string1, string2, etc., it returns a string consisting of the characters of this object (converted to a string) followed by the characters of each of string1, string2, etc. (where each argument is converted to a string). The result is a string value, not a String object. The following steps are taken:
1. Call ToString, giving it the this value as its argument.
2. Let R be Result(1).
3. Get the next argument in the argument list; if there are no more arguments, go to step 7.
4. Call ToString(Result(3)).
5. Let R be the string value consisting of the characters in the previous value of R followed by the characters Result(4).
6. Go to step 3.
7. Return R.
The length property of the concat method is 1.
NOTE
The concat function is intentionally
generic; it does not require that its this value
be a String object. Therefore it can be transferred to other kinds of
objects for use as a method.
If searchString appears as a substring of the result of converting this object to a string, at one or more positions that are greater than or equal to position, then the index of the smallest such position is returned; otherwise, -1 is returned. If position is undefined, 0 is assumed, so as to search all of the string.
The indexOf method takes two arguments, searchString and position, and performs the following steps:
1. Call ToString, giving it the this value as its argument.
2. Call ToString(searchString).
3. Call ToInteger(position). (If position is undefined, this step produces the value 0).
4. Compute the number of characters in Result(1).
5. Compute min(max(Result(3), 0), Result(4)).
6. Compute the number of characters in the string that is Result(2).
7. Compute the smallest possible integer k not smaller than Result(5) such that k+ Result(6) is not greater than Result(4), and for all nonnegative integers j less than Result(6), the character at position k+ j of Result(1) is the same as the character at position j of Result(2); but if there is no such integer k, then compute the value -1.
8. Return Result(7).
The length property of the indexOf method is 1.
NOTE
The indexOf function is intentionally
generic; it does not require that its this value
be a String object. Therefore, it can be transferred to other kinds of
objects for use as a method.
If searchString appears as a substring of the result of converting this object to a string at one or more positions that are smaller than or equal to position, then the index of the greatest such position is returned; otherwise, -1 is returned. If position is undefined, the length of the string value is assumed, so as to search all of the string.
The lastIndexOf method takes two arguments, searchString and position, and performs the following steps:
1. Call ToString, giving it the this value as its argument.
2. Call ToString(searchString).
3. Call ToNumber(position). (If position is undefined, this step produces the value NaN).
4. If Result(3) is NaN, use +∞; otherwise, call ToInteger(Result(3)).
5. Compute the number of characters in Result(1).
6. Compute min(max(Result(4), 0), Result(5)).
7. Compute the number of characters in the string that is Result(2).
8. Compute the largest possible nonnegative integer k not larger than Result(6) such that k+ Result(7) is not greater than Result(5), and for all nonnegative integers j less than Result(7), the character at position k+ j of Result(1) is the same as the character at position j of Result(2); but if there is no such integer k, then compute the value -1.
9. Return Result(8).
The length property of the lastIndexOf method is 1.
NOTE
The lastIndexOf function is intentionally
generic; it does not require that its this value
be a String object. Therefore, it can be transferred to other kinds of
objects for use as a method.
When the localeCompare method is called with one argument that, it returns a number other than NaN that represents the result of a locale-sensitive string comparison of this object (converted to a string) with that (converted to a string). The two strings are compared in an implementation-defined fashion. The result is intended to order strings in the sort order specified by the system default locale, and will be negative, zero, or positive, depending on whether this comes before that in the sort order, the strings are equal, or this comes after that in the sort order, respectively.
The localeCompare method, if considered as a function of two arguments this and that, is a consistent comparison function (as defined in 15.4.4.11) on the set of all strings. Furthermore, localeCompare returns 0 or -0 when comparing two strings that are considered canonically equivalent by the Unicode standard.
The actual return values are left implementation-defined to permit implementers to encode additional information in the result value, but the function is required to define a total ordering on all strings and to return 0 when comparing two strings that are considered canonically equivalent by the Unicode standard.
NOTE 1
The localeCompare method itself is not
directly suitable as an argument to
Array.prototype.sort because the latter
requires a function of two arguments.
NOTE 2
This function is intended to rely on whatever language-sensitive
comparison functionality is available to the ECMAScript environment
from the host environment, and to compare according to the rules of the
host environment's current locale. It is strongly recommended that this
function treat strings that are canonically equivalent according to the
Unicode standard as identical (in other words, compare the strings as
if they had both been converted to Normalised Form C or D first). It is
also recommended that this function not honour Unicode compatibility
equivalences or decompositions.
If no language-sensitive comparison at all is available from the host environment, this function may perform a bitwise comparison.
NOTE 3
The localeCompare function is
intentionally generic; it does not require that its
this value be a String object. Therefore, it can be
transferred to other kinds of objects for use as a method.
NOTE 4
The second 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.
If regexp is not an object whose [[Class]] property is "RegExp", it is replaced with the result of the expression new RegExp( regexp). Let string denote the result of converting the this value to a string. Then do one of the following:
null; otherwise, the value returned
is an array with the length property set to n and properties 0 through n1 corresponding
to the first elements of the results of all matching invocations of RegExp.prototype.exec.NOTE
The match function is intentionally
generic; it does not require that its this value
be a String object. Therefore, it can be transferred to other kinds of
objects for use as a method.
Let string denote the result of converting the this value to a string.
If searchValue is a regular expression (an object whose [[Class]] property is "RegExp"), do the following: If searchValue. global is false, then search string for the first match of the regular expression searchValue. If searchValue.global is true, then search string for all matches of the regular expression searchValue. Do the search in the same manner as in String.prototype.match, including the update of searchValue. lastIndex. Let m be the number of left capturing parentheses in searchValue (NCapturingParens as specified in 15.10.2.1).
If searchValue is not a regular expression, let searchString be ToString(searchValue) and search string for the first occurrence of searchString. Let m be 0.
If replaceValue is a function, then for each matched substring, call the function with the following m + 3 arguments. Argument 1 is the substring that matched. If searchValue is a regular expression, the next m arguments are all of the captures in the MatchResult (see 15.10.2.1). Argument m + 2 is the offset within string where the match occurred, and argument m + 3 is string. The result is a string value derived from the original input by replacing each matched substring with the corresponding return value of the function call, converted to a string if need be.
Otherwise, let newstring denote the result of converting replaceValue to a string. The result is a string value derived from the original input string by replacing each matched substring with a string derived from newstring by replacing characters in newstring by replacement text as specified in the following table. These $ replacements are done left-to-right, and, once such a replacement is performed, the new replacement text is not subject to further replacements. For example, "$1,$2".replace(/(\$(\d))/g, "$$1-$1$2") returns "$1-$11,$1-$22". A $ in newstring that does not match any of the forms below is left as is.
| Characters | Replacement text |
|---|---|
| $$ | $ |
| $& | The matched substring. |
| $` | The portion of string that precedes the matched substring. |
| $' | The portion of string that follows the matched substring. |
| $n | The nth capture, where n is a single digit 1-9 and $n is not followed by a decimal digit. If n <= m and the nth capture is undefined, use the empty string instead. If n> m, the result is implementation-defined. |
| $nn | The nnth capture, where nn is a two-digit decimal number 01-99. If nn<= m and the nn th capture is undefined, use the empty string instead. If nn> m, the result is implementation-defined. |
NOTE
The replace function is intentionally
generic; it does not require that its this value
be a String object. Therefore, it can be transferred to other kinds of
objects for use as a method.
If regexp is not an object whose [[Class]] property is "RegExp", it is replaced with the result of the expression new RegExp( regexp). Let string denote the result of converting the this value to a string. The value string is searched from its beginning for an occurrence of the regular expression pattern regexp. The result is a number indicating the offset within the string where the pattern matched, or -1 if there was no match.
NOTE 1
This method ignores the lastIndex and
global properties of regexp. The
lastIndex property of regexp is left
unchanged.
NOTE 2
The search function is intentionally
generic; it does not require that its this value
be a String object. Therefore, it can be transferred to other kinds of
objects for use as a method.
The slice method takes two arguments, start and end, and returns a substring of the result of converting this object to a string, starting from character position start and running to, but not including, character position end (or through the end of the string if end is undefined). If start is negative, it is treated as (sourceLength+ start) where sourceLength is the length of the string. If end is negative, it is treated as (sourceLength+ end) where sourceLength is the length of the string. The result is a string value, not a String object. The following steps are taken:
1. Call ToString, giving it the this value as its argument.
2. Compute the number of characters in Result(1).
3. Call ToInteger(start).
4. If end is undefined, use Result(2); else use ToInteger(end).
5. If Result(3) is negative, use max(Result(2)+ Result(3), 0); else use min(Result(3), Result(2)).
6. If Result(4) is negative, use max(Result(2)+ Result(4), 0); else use min(Result(4), Result(2)).
7. Compute max(Result(6)- Result(5), 0).
8. Return a string containing Result(7) consecutive characters from Result(1) beginning with the character at position Result(5).
The length property of the slice method is 2.
NOTE