Reference numbe
r
ECMA-123:2009
©
Ecma International 2009
ECMA-262
5
th
Edition / December 2009
ECMAScript Language
Specification
COPYRIGHT PROTECTED DOCUMENT
© Ecma International 2009
© Ecma International 2009 i
Contents Page
Introduction ....................................................................................................................................................... vii
1
Scope ...................................................................................................................................................... 1
2 Conformance ......................................................................................................................................... 1
3 Normative references ............................................................................................................................ 1
4 Overview ................................................................................................................................................. 1
4.1 Web Scripting ........................................................................................................................................ 2
4.2 Language Overview ............................................................................................................................... 2
4.2.1 Objects.................................................................................................................................................... 3
4.2.2 The Strict Variant of ECMAScript ........................................................................................................ 4
4.3 Definitions .............................................................................................................................................. 4
5 Notational Conventions ........................................................................................................................ 7
5.1 Syntactic and Lexical Grammars ......................................................................................................... 7
5.1.1 Context-Free Grammars ....................................................................................................................... 7
5.1.2 The Lexical and RegExp Grammars .................................................................................................... 8
5.1.3 The Numeric String Grammar .............................................................................................................. 8
5.1.4 The Syntactic Grammar ........................................................................................................................ 8
5.1.5 The JSON Grammar .............................................................................................................................. 9
5.1.6 Grammar Notation ................................................................................................................................. 9
5.2 Algorithm Conventions ....................................................................................................................... 11
6 Source Text .......................................................................................................................................... 12
7 Lexical Conventions ............................................................................................................................ 13
7.1 Unicode Format-Control Characters ................................................................................................. 14
7.2 White Space ......................................................................................................................................... 14
7.3 Line Terminators ................................................................................................................................. 15
7.4 Comments ............................................................................................................................................ 16
7.5 Tokens .................................................................................................................................................. 17
7.6 Identifier Names and Identifiers ......................................................................................................... 17
7.6.1 Reserved Words .................................................................................................................................. 18
7.7 Punctuators .......................................................................................................................................... 19
7.8 Literals .................................................................................................................................................. 19
7.8.1 Null Literals .......................................................................................................................................... 19
7.8.2 Boolean Literals ................................................................................................................................... 20
7.8.3 Numeric Literals .................................................................................................................................. 20
7.8.4 String Literals ...................................................................................................................................... 22
7.8.5 Regular Expression Literals ............................................................................................................... 24
7.9 Automatic Semicolon Insertion ......................................................................................................... 25
7.9.1 Rules of Automatic Semicolon Insertion .......................................................................................... 26
7.9.2 Examples of Automatic Semicolon Insertion ................................................................................... 27
8 Types .................................................................................................................................................... 28
8.1 The Undefined Type ............................................................................................................................ 28
8.2 The Null Type ....................................................................................................................................... 28
8.3 The Boolean Type ................................................................................................................................ 28
8.4 The String Type ................................................................................................................................... 28
8.5 The Number Type ................................................................................................................................ 29
8.6 The Object Type ................................................................................................................................... 30
8.6.1 Property Attributes .............................................................................................................................. 30
8.6.2 Object Internal Properties and Methods ........................................................................................... 31
8.7 The Reference Specification Type ..................................................................................................... 34
ii © Ecma International 2009
8.7.1
GetValue (V) ..........................................................................................................................................35
8.7.2 PutValue (V, W) ....................................................................................................................................35
8.8 The List Specification Type ................................................................................................................36
8.9 The Completion Specification Type ...................................................................................................36
8.10 The Property Descriptor and Property Identifier Specification Types ...........................................37
8.10.1 IsAccessorDescriptor ( Desc ) ............................................................................................................37
8.10.2 IsDataDescriptor ( Desc ) ....................................................................................................................37
8.10.3 IsGenericDescriptor ( Desc ) ..............................................................................................................37
8.10.4 FromPropertyDescriptor ( Desc ) .......................................................................................................38
8.10.5 ToPropertyDescriptor ( Obj ) ..............................................................................................................38
8.11 The Lexical Environment and Environment Record Specification Types .....................................39
8.12 Algorithms for Object Internal Methods ............................................................................................39
8.12.1 [[GetOwnProperty]] (P) ........................................................................................................................39
8.12.2 [[GetProperty]] (P) ................................................................................................................................39
8.12.3 [[Get]] (P) ..............................................................................................................................................39
8.12.4 [[CanPut]] (P) ........................................................................................................................................39
8.12.5 [[Put]] ( P, V, Throw ) ...........................................................................................................................40
8.12.6 [[HasProperty]] (P) ...............................................................................................................................40
8.12.7 [[Delete]] (P, Throw) .............................................................................................................................40
8.12.8 [[DefaultValue]] (hint) ..........................................................................................................................41
8.12.9 [[DefineOwnProperty]] (P, Desc, Throw) ...........................................................................................41
9 Type Conversion and Testing ............................................................................................................42
9.1 ToPrimitive ...........................................................................................................................................43
9.2 ToBoolean ............................................................................................................................................43
9.3 ToNumber .............................................................................................................................................43
9.3.1 ToNumber Applied to the String Type ...............................................................................................44
9.4 ToInteger ...............................................................................................................................................46
9.5 ToInt32: (Signed 32 Bit Integer) ..........................................................................................................46
9.6 ToUint32: (Unsigned 32 Bit Integer)...................................................................................................47
9.7 ToUint16: (Unsigned 16 Bit Integer)...................................................................................................47
9.8 ToString ................................................................................................................................................47
9.8.1 ToString Applied to the Number Type ...............................................................................................48
9.9 ToObject ...............................................................................................................................................49
9.10 CheckObjectCoercible ........................................................................................................................49
9.11 IsCallable ..............................................................................................................................................49
9.12 The SameValue Algorithm ..................................................................................................................50
10 Executable Code and Execution Contexts ........................................................................................50
10.1 Types of Executable Code ..................................................................................................................50
10.1.1 Strict Mode Code .................................................................................................................................51
10.2 Lexical Environments ..........................................................................................................................51
10.2.1 Environment Records .........................................................................................................................51
10.2.2 Lexical Environment Operations ........................................................................................................56
10.2.3 The Global Environment .....................................................................................................................56
10.3 Execution Contexts .............................................................................................................................56
10.3.1 Identifier Resolution ............................................................................................................................57
10.4 Establishing an Execution Context ....................................................................................................57
10.4.1 Entering Global Code ..........................................................................................................................58
10.4.2 Entering Eval Code ..............................................................................................................................58
10.4.3 Entering Function Code ......................................................................................................................58
10.5 Declaration Binding Instantiation ......................................................................................................59
10.6 Arguments Object ................................................................................................................................60
11 Expressions ..........................................................................................................................................62
11.1 Primary Expressions ...........................................................................................................................62
11.1.1 The this Keyword ...............................................................................................................................62
11.1.2 Identifier Reference .............................................................................................................................63
11.1.3 Literal Reference ..................................................................................................................................63
11.1.4 Array Initialiser .....................................................................................................................................63
11.1.5 Object Initialiser ...................................................................................................................................64
© Ecma International 2009 iii
11.1.6
The Grouping Operator ....................................................................................................................... 66
11.2 Left-Hand-Side Expressions .............................................................................................................. 66
11.2.1 Property Accessors ............................................................................................................................. 67
11.2.2 The new Operator................................................................................................................................. 68
11.2.3 Function Calls ...................................................................................................................................... 68
11.2.4 Argument Lists .................................................................................................................................... 68
11.2.5 Function Expressions ......................................................................................................................... 69
11.3 Postfix Expressions ............................................................................................................................ 69
11.3.1 Postfix Increment Operator ................................................................................................................ 69
11.3.2 Postfix Decrement Operator ............................................................................................................... 69
11.4 Unary Operators .................................................................................................................................. 70
11.4.1 The delete Operator .......................................................................................................................... 70
11.4.2 The void Operator .............................................................................................................................. 70
11.4.3 The typeof Operator .......................................................................................................................... 71
11.4.4 Prefix Increment Operator .................................................................................................................. 71
11.4.5 Prefix Decrement Operator ................................................................................................................. 71
11.4.6 Unary + Operator ................................................................................................................................. 72
11.4.7 Unary - Operator ................................................................................................................................. 72
11.4.8 Bitwise NOT Operator ( ~ ) .................................................................................................................. 72
11.4.9 Logical NOT Operator ( ! ) .................................................................................................................. 72
11.5 Multiplicative Operators ...................................................................................................................... 72
11.5.1 Applying the * Operator ..................................................................................................................... 73
11.5.2 Applying the / Operator ..................................................................................................................... 73
11.5.3 Applying the % Operator ..................................................................................................................... 74
11.6 Additive Operators .............................................................................................................................. 74
11.6.1 The Addition operator ( + ) ................................................................................................................. 74
11.6.2 The Subtraction Operator ( - ) ........................................................................................................... 75
11.6.3 Applying the Additive Operators to Numbers .................................................................................. 75
11.7 Bitwise Shift Operators ....................................................................................................................... 75
11.7.1 The Left Shift Operator ( << ) .............................................................................................................. 75
11.7.2 The Signed Right Shift Operator ( >> ) .............................................................................................. 76
11.7.3 The Unsigned Right Shift Operator ( >>> ) ....................................................................................... 76
11.8 Relational Operators ........................................................................................................................... 77
11.8.1 The Less-than Operator ( < ) .............................................................................................................. 77
11.8.2 The Greater-than Operator ( > ) .......................................................................................................... 77
11.8.3 The Less-than-or-equal Operator ( <= ) ............................................................................................. 78
11.8.4 The Greater-than-or-equal Operator ( >= ) ........................................................................................ 78
11.8.5 The Abstract Relational Comparison Algorithm .............................................................................. 78
11.8.6 The instanceof operator...................................................................................................................... 79
11.8.7 The in operator .................................................................................................................................... 79
11.9 Equality Operators .............................................................................................................................. 79
11.9.1 The Equals Operator ( == ) ................................................................................................................. 80
11.9.2 The Does-not-equals Operator ( != ) ................................................................................................. 80
11.9.3 The Abstract Equality Comparison Algorithm ................................................................................. 80
11.9.4 The Strict Equals Operator ( === ) ..................................................................................................... 81
11.9.5 The Strict Does-not-equal Operator ( !== ) ...................................................................................... 81
11.9.6 The Strict Equality Comparison Algorithm ....................................................................................... 81
11.10 Binary Bitwise Operators.................................................................................................................... 82
11.11 Binary Logical Operators.................................................................................................................... 82
11.12 Conditional Operator ( ? : ) ............................................................................................................. 83
11.13 Assignment Operators ........................................................................................................................ 84
11.13.1Simple Assignment ( = ) ...................................................................................................................... 84
11.13.2Compound Assignment ( op= ) .......................................................................................................... 84
11.14 Comma Operator ( , ) ....................................................................................................................... 85
12 Statements ........................................................................................................................................... 86
12.1 Block ..................................................................................................................................................... 86
12.2 Variable Statement .............................................................................................................................. 87