1、 Reference number ECMA-123:2009 Ecma International 2009 ECMA-262 6th Edition / June 2015 ECMAScript 2015 Language Specification COPYRIGHT PROTECTED DOCUMENT Ecma International 2015 Ecma International Rue du Rhone 114 CH-1204 Geneva Tel: +41 22 849 6000 Fax: +41 22 849 6001 Web: http:/www.ecma-intern
2、ational.org Ecma International 2015 i COPYRIGHT NOTICE 2015 Ecma International This document may be copied, published and distributed to others, and certain derivative works of it may be prepared, copied, published, and distributed, in whole or in part, provided that the above copyright notice and t
3、his Copyright License and Disclaimer are included on all such copies and derivative works. The only derivative works that are permissible under this Copyright License and Disclaimer are: (i) works which incorporate all or portion of this document for the purpose of providing commentary or explanatio
4、n (such as an annotated version of the document), (ii) works which incorporate all or portion of this document for the purpose of incorporating features that provide accessibility, (iii) translations of this document into languages other than English and into differ ent formats and (iv) works by mak
5、ing use of this specification in standard conformant products by implementing (e.g. by copy and paste wholly or partly) the functionality therein. However, the content of this document itself may not be modified in any way, including by removing the copyright notice or references to Ecma Internation
6、al, except as required to translate it into languages other than English or into a different format. The official version of an Ecma International document is the English language version on the Ecma International website. In the event of discrepancies between a translated version and the official v
7、ersion, the official version shall govern. The limited permissions granted above are perpetual and will not be revoked by Ecma Internat ional or its successors or assigns. This document and the information contained herein is provided on an “AS IS“ basis and ECMA INTERNATIONAL DISCLAIMS ALL WARRANTI
8、ES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION HEREIN WILL NOT INFRINGE ANY OWNERSHIP RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.“ Software License All Software contained in this document (“Software)“ is
9、protected by copyright and is being made available under the “BSD License“, included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license ev
10、en if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT http:/www.ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDAR
11、DS*. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in bi
12、nary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the authors nor Ecma International may be used to endorse or promote products derived from this
13、 software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL “AS IS“ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECMA
14、 INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
15、CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ii Ecma International 2015 Ecma International 2015 iii Contents Page 1 Scope .1 2 Conformance .1 3 Normative references 1 4 O
16、verview 1 4.1 Web Scripting 2 4.2 ECMAScript Overview .2 Objects.3 4.2.1The Strict Variant of ECMAScript .4 4.2.24.3 Terms and definitions .5 4.4 Organization of This Specification .8 5 Notational Conventions 8 5.1 Syntactic and Lexical Grammars 8 Context-Free Grammars8 5.1.1The Lexical and RegExp G
17、rammars .9 5.1.2The Numeric String Grammar .9 5.1.3The Syntactic Grammar 9 5.1.4Grammar Notation . 10 5.1.55.2 Algorithm Conventions . 14 5.3 Static Semantic Rules . 16 6 ECMAScript Data Types and Values 16 6.1 ECMAScript Language Types . 16 The Undefined Type 16 6.1.1The Null Type. 17 6.1.2The Bool
18、ean Type . 17 6.1.3The String Type . 17 6.1.4The Symbol Type. 17 6.1.5The Number Type 18 6.1.6The Object Type 19 6.1.76.2 ECMAScript Specification Types 29 The List and Record Specification Type 29 6.2.1The Completion Record Specification Type 30 6.2.2The Reference Specification Type 31 6.2.3The Pro
19、perty Descriptor Specification Type 33 6.2.4The Lexical Environment and Environment Record Specification Types 35 6.2.5Data Blocks . 35 6.2.67 Abstract Operations 36 7.1 Type Conversion . 36 ToPrimitive ( input , PreferredType ) 36 7.1.1ToBoolean ( argument ) 37 7.1.2ToNumber ( argument ) . 38 7.1.3
20、ToInteger ( argument ) 40 7.1.4ToInt32 ( argument ) 40 7.1.5ToUint32 ( argument ) . 41 7.1.6ToInt16 ( argument ) 41 7.1.7ToUint16 ( argument ) . 41 7.1.8ToInt8 ( argument ) 41 7.1.9ToUint8 ( argument ) . 42 7.1.10ToUint8Clamp ( argument ) . 42 7.1.11ToString ( argument ) 42 7.1.12iv Ecma Internation
21、al 2015 ToObject ( argument ) . 43 7.1.13ToPropertyKey ( argument ) . 44 7.1.14ToLength ( argument ) 44 7.1.15CanonicalNumericIndexString ( argument ) 44 7.1.167.2 Testing and Comparison Operations . 45 RequireObjectCoercible ( argument ) 45 7.2.1IsArray ( argument ) 45 7.2.2IsCallable ( argument )
22、45 7.2.3IsConstructor ( argument ) . 45 7.2.4IsExtensible (O) . 45 7.2.5IsInteger ( argument ) 46 7.2.6IsPropertyKey ( argument ) 46 7.2.7IsRegExp ( argument ) 46 7.2.8SameValue(x, y) 46 7.2.9SameValueZero(x, y) . 46 7.2.10Abstract Relational Comparison 47 7.2.11Abstract Equality Comparison . 48 7.2
23、.12Strict Equality Comparison 48 7.2.137.3 Operations on Objects 49 Get (O, P) . 49 7.3.1GetV (V, P) . 49 7.3.2Set (O, P, V, Throw) . 49 7.3.3CreateDataProperty (O, P, V) 49 7.3.4CreateMethodProperty (O, P, V) . 50 7.3.5CreateDataPropertyOrThrow (O, P, V) 50 7.3.6DefinePropertyOrThrow (O, P, desc) 5
24、0 7.3.7DeletePropertyOrThrow (O, P) 50 7.3.8GetMethod (O, P) . 51 7.3.9HasProperty (O, P) 51 7.3.10HasOwnProperty (O, P) . 51 7.3.11Call(F, V, argumentsList) 51 7.3.12Construct (F, argumentsList, newTarget) . 51 7.3.13SetIntegrityLevel (O, level) . 52 7.3.14TestIntegrityLevel (O, level) 52 7.3.15Cre
25、ateArrayFromList (elements) 52 7.3.16CreateListFromArrayLike (obj , elementTypes ) 53 7.3.17Invoke(O,P, argumentsList) 53 7.3.18OrdinaryHasInstance (C, O) 53 7.3.19SpeciesConstructor ( O, defaultConstructor ) . 54 7.3.20EnumerableOwnNames (O) 54 7.3.21GetFunctionRealm ( obj ) 54 7.3.227.4 Operations
26、 on Iterator Objects . 54 GetIterator ( obj, method ) 55 7.4.1IteratorNext ( iterator, value ) 55 7.4.2IteratorComplete ( iterResult ) 55 7.4.3IteratorValue ( iterResult ) . 55 7.4.4IteratorStep ( iterator ) 55 7.4.5IteratorClose( iterator, completion ) . 55 7.4.6CreateIterResultObject ( value, done
27、 ) . 56 7.4.7CreateListIterator ( list ) 56 7.4.88 Executable Code and Execution Contexts. 56 8.1 Lexical Environments . 56 Environment Records . 57 8.1.1Lexical Environment Operations 71 8.1.28.2 Code Realms . 72 CreateRealm ( ) 73 8.2.1CreateIntrinsics ( realmRec ) 73 8.2.2SetRealmGlobalObject ( r
28、ealmRec, globalObj ) . 73 8.2.3SetDefaultGlobalBindings ( realmRec ) . 73 8.2.4 Ecma International 2015 v 8.3 Execution Contexts . 74 ResolveBinding ( name, env ). 75 8.3.1GetThisEnvironment ( ) . 75 8.3.2ResolveThisBinding ( ) 75 8.3.3GetNewTarget ( ) 75 8.3.4GetGlobalObject ( ) 76 8.3.58.4 Jobs an
29、d Job Queues . 76 EnqueueJob (queueName, job, arguments) . 77 8.4.1NextJob result . 77 8.4.28.5 ECMAScript Initialization() 77 InitializeHostDefinedRealm ( realm ) 78 8.5.19 Ordinary and Exotic Objects Behaviours . 78 9.1 Ordinary Object Internal Methods and Internal Slots 78 GetPrototypeOf ( ). 78
30、9.1.1SetPrototypeOf (V) 78 9.1.2IsExtensible ( ) 79 9.1.3PreventExtensions ( ) . 79 9.1.4GetOwnProperty (P) . 79 9.1.5DefineOwnProperty (P, Desc) 79 9.1.6HasProperty(P) . 81 9.1.7Get (P, Receiver) . 81 9.1.8Set ( P, V, Receiver) 81 9.1.9Delete (P) . 82 9.1.10Enumerate () . 82 9.1.11OwnPropertyKeys (
31、 ) 83 9.1.12ObjectCreate(proto, internalSlotsList) . 83 9.1.13OrdinaryCreateFromConstructor ( constructor, intrinsicDefaultProto, internalSlotsList ) . 83 9.1.14GetPrototypeFromConstructor ( constructor, intrinsicDefaultProto ) 84 9.1.159.2 ECMAScript Function Objects 84 Call ( thisArgument, argumen
32、tsList) 85 9.2.1Construct ( argumentsList, newTarget) . 86 9.2.2FunctionAllocate (functionPrototype, strict ,functionKind ) . 87 9.2.3FunctionInitialize (F, kind, ParameterList, Body, Scope) 87 9.2.4FunctionCreate (kind, ParameterList, Body, Scope, Strict, prototype) . 88 9.2.5GeneratorFunctionCreat
33、e (kind, ParameterList, Body, Scope, Strict) 88 9.2.6AddRestrictedFunctionProperties ( F, realm ) . 88 9.2.7MakeConstructor (F, writablePrototype, prototype) 88 9.2.8MakeClassConstructor ( F) . 89 9.2.9MakeMethod ( F, homeObject) 89 9.2.10SetFunctionName (F, name, prefix) 89 9.2.11FunctionDeclaratio
34、nInstantiation(func, argumentsList) 89 9.2.129.3 Built-in Function Objects 92 Call ( thisArgument, argumentsList) 92 9.3.1Construct (argumentsList, newTarget) 93 9.3.2CreateBuiltinFunction(realm, steps, prototype, internalSlotsList) . 93 9.3.39.4 Built-in Exotic Object Internal Methods and Slots . 9
35、3 Bound Function Exotic Objects 93 9.4.1Array Exotic Objects . 95 9.4.2String Exotic Objects 97 9.4.3Arguments Exotic Objects 98 9.4.4Integer Indexed Exotic Objects. 102 9.4.5Module Namespace Exotic Objects 105 9.4.69.5 Proxy Object Internal Methods and Internal Slots . 108 GetPrototypeOf ( ). 109 9
36、.5.1SetPrototypeOf (V) 109 9.5.2IsExtensible ( ) 110 9.5.3PreventExtensions ( ) . 110 9.5.4GetOwnProperty (P) . 111 9.5.5DefineOwnProperty (P, Desc) 111 9.5.6vi Ecma International 2015 HasProperty (P) 112 9.5.7Get (P, Receiver) . 113 9.5.8Set ( P, V, Receiver) 113 9.5.9Delete (P) 114 9.5.10Enumerate
37、 () . 114 9.5.11OwnPropertyKeys ( ) 114 9.5.12Call (thisArgument, argumentsList) 115 9.5.13Construct ( argumentsList, newTarget). 116 9.5.14ProxyCreate(target, handler) 116 9.5.1510 ECMAScript Language: Source Code 116 10.1 Source Text . 116 Static Semantics: UTF16Encoding ( cp ) 117 10.1.1Static Se
38、mantics: UTF16Decode( lead, trail ). 117 10.1.210.2 Types of Source Code 117 Strict Mode Code 118 10.2.1Non-ECMAScript Functions 118 10.2.211 ECMAScript Language: Lexical Grammar . 118 11.1 Unicode Format-Control Characters 119 11.2 White Space 120 11.3 Line Terminators . 121 11.4 Comments . 121 11.
39、5 Tokens . 122 11.6 Names and Keywords . 122 Identifier Names 123 11.6.1Reserved Words 124 11.6.211.7 Punctuators . 125 11.8 Literals . 125 Null Literals . 125 11.8.1Boolean Literals 125 11.8.2Numeric Literals 125 11.8.3String Literals 128 11.8.4Regular Expression Literals . 131 11.8.5Template Liter
40、al Lexical Components 132 11.8.611.9 Automatic Semicolon Insertion 134 Rules of Automatic Semicolon Insertion . 135 11.9.1Examples of Automatic Semicolon Insertion. 136 11.9.212 ECMAScript Language: Expressions . 137 12.1 Identifiers 137 Static Semantics: Early Errors 137 12.1.1Static Semantics: Bou
41、ndNames . 138 12.1.2Static Semantics: IsValidSimpleAssignmentTarget . 138 12.1.3Static Semantics: StringValue 138 12.1.4Runtime Semantics: BindingInitialization 138 12.1.5Runtime Semantics: Evaluation 139 12.1.612.2 Primary Expression 139 Semantics 140 12.2.1The this Keyword 141 12.2.2Identifier Ref
42、erence . 141 12.2.3Literals . 141 12.2.4Array Initializer 142 12.2.5Object Initializer 144 12.2.6Function Defining Expressions 148 12.2.7Regular Expression Literals . 148 12.2.8Template Literals 148 12.2.9The Grouping Operator . 152 12.2.1012.3 Left-Hand-Side Expressions . 153 Static Semantics . 153
43、 12.3.1Property Accessors 156 12.3.2 Ecma International 2015 vii The new Operator 157 12.3.3Function Calls . 157 12.3.4The super Keyword 159 12.3.5Argument Lists 160 12.3.6Tagged Templates . 160 12.3.7Meta Properties . 161 12.3.812.4 Postfix Expressions 161 Static Semantics: Early Errors. 161 12.4.1
44、Static Semantics: IsFunctionDefinition . 161 12.4.2Static Semantics: IsValidSimpleAssignmentTarget . 161 12.4.3Postfix Increment Operator. 162 12.4.4Postfix Decrement Operator . 162 12.4.512.5 Unary Operators 162 Static Semantics: Early Errors. 162 12.5.1Static Semantics: IsFunctionDefinition . 162
45、12.5.2Static Semantics: IsValidSimpleAssignmentTarget . 163 12.5.3The delete Operator 163 12.5.4The void Operator 164 12.5.5The typeof Operator 164 12.5.6Prefix Increment Operator 165 12.5.7Prefix Decrement Operator . 165 12.5.8Unary + Operator . 165 12.5.9Unary - Operator . 165 12.5.10Bitwise NOT O
46、perator ( ) 166 12.5.11Logical NOT Operator ( ! ) 166 12.5.1212.6 Multiplicative Operators 166 Static Semantics: IsFunctionDefinition . 166 12.6.1Static Semantics: IsValidSimpleAssignmentTarget . 166 12.6.2Runtime Semantics: Evaluation 166 12.6.312.7 Additive Operators 168 Static Semantics: IsFuncti
47、onDefinition . 168 12.7.1Static Semantics: IsValidSimpleAssignmentTarget . 168 12.7.2The Addition operator ( + ) 169 12.7.3The Subtraction Operator ( - ) 169 12.7.4Applying the Additive Operators to Numbers 169 12.7.512.8 Bitwise Shift Operators . 170 Static Semantics: IsFunctionDefinition . 170 12.
48、8.1Static Semantics: IsValidSimpleAssignmentTarget . 170 12.8.2The Left Shift Operator ( ) . 171 12.8.4The Unsigned Right Shift Operator ( ) . 171 12.8.512.9 Relational Operators . 172 Static Semantics: IsFunctionDefinition . 172 12.9.1Static Semantics: IsValidSimpleAssignmentTarget . 172 12.9.2Runt
49、ime Semantics: Evaluation 172 12.9.3Runtime Semantics: InstanceofOperator(O, C) . 174 12.9.412.10 Equality Operators 174 Static Semantics: IsFunctionDefinition . 174 12.10.1Static Semantics: IsValidSimpleAssignmentTarget . 174 12.10.2Runtime Semantics: Evaluation 174 12.10.312.11 Binary Bitwise Operators 176 Static Semantics: IsFunctionDefinition . 176 12.11.1Static Semantics: IsValidSimpleAssignmentTarget . 176 12.11.2Runtime Semantics: