Docsity
Docsity

Prepare-se para as provas
Prepare-se para as provas

Estude fácil! Tem muito documento disponível na Docsity


Ganhe pontos para baixar
Ganhe pontos para baixar

Ganhe pontos ajudando outros esrudantes ou compre um plano Premium


Guias e Dicas
Guias e Dicas

Ecma 262, Notas de estudo de Sistemas de Informação

Padrão ECMA - Especificação de linguagens de script. Em inglês.

Tipologia: Notas de estudo

Antes de 2010

Compartilhado em 03/10/2009

ri-va-3
ri-va-3 🇧🇷

4 documentos

1 / 188

Toggle sidebar

Esta página não é visível na pré-visualização

Não perca as partes importantes!

bg1
Standard ECMA-262
3rd Edition - December 1999
Standardizing Information and Communication Systems
Phone: +41 22 849.60.00 - Fax: +41 22 849.60.01 - URL: http://www.ecma.ch - Internet: helpdesk@ecma.ch
ECMAScript Language
Specification
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Pré-visualização parcial do texto

Baixe Ecma 262 e outras Notas de estudo em PDF para Sistemas de Informação, somente na Docsity!

Standard ECMA-

3 r d^ Edition - December 1999

S t a n d a r d i z i n g I n f o r m a t i o n a n d C o m m u n i c a t i o n S y s t e m s

P h o n e : + 4 1 2 2 8 4 9. 6 0. 0 0 - F a x : + 4 1 2 2 8 4 9. 6 0. 0 1 - U R L : h t t p : / / w w w. e c m a. c h - I n t e r n e t : h e l p d e s k @ e c m a. c h

ECMAScript Language

Specification

Brief History

This ECMA Standard is based on several originating technologies, the most well known being JavaScript (Netscape) and JScript (Microsoft). The language was invented by Brendan Eich at Netscape and first appeared in that company’s Navigator 2.0 browser. It has appeared in all subsequent browsers from Netscape and in all browsers from Microsoft starting with Internet Explorer 3.0.

The development of this Standard started in November 1996. The first edition of this ECMA Standard was adopted by the ECMA General Assembly of June 1997.

That ECMA Standard was submitted to ISO/IEC JTC 1 for adoption under the fast-track procedure, and approved as international standard ISO/IEC 16262, in April 1998. The ECMA General Assembly of June 1998 approved the second edition of ECMA-262 to keep it fully aligned with ISO/IEC 16262. Changes between the first and the second edition are editorial in nature.

The current document defines the third edition of the Standard and includes powerful regular expressions, better string handling, new control statements, try/catch exception handling, tighter definition of errors, formatting for numeric output and minor changes in anticipation of forthcoming internationalisation facilities and future language growth.

Work on the language is not complete. The technical committee is working on significant enhancements, including mechanisms for scripts to be created and used across the Internet, and tighter coordination with other standards bodies such as groups within the World Wide Web Consortium and the Wireless Application Protocol Forum.

This Standard has been adopted as 3rd Edition of ECMA-262 by the ECMA General Assembly in December, 1999.

- i -

1 Scope

This Standard defines the ECMAScript scripting language.

2 Conformance

A conforming implementation of ECMAScript must provide and support all the types, values, objects, properties, functions, and program syntax and semantics described in this specification. A conforming implementation of this International standard shall interpret characters in conformance with the Unicode Standard, Version 2.1 or later, and ISO/IEC 10646-1 with either UCS-2 or UTF-16 as the adopted encoding form, implementation level 3. If the adopted ISO/IEC 10646-1 subset is not otherwise specified, it is presumed to be the BMP subset, collection 300. If the adopted encoding form is not otherwise specified, it presumed to be the UTF-16 encoding form. A conforming implementation of ECMAScript is permitted to provide additional types, values, objects, properties, and functions beyond those described in this specification. In particular, a conforming implementation of ECMAScript is permitted to provide properties not described in this specification, and values for those properties, for objects that are described in this specification. A conforming implementation of ECMAScript is permitted to support program and regular expression syntax not described in this specification. In particular, a conforming implementation of ECMAScript is permitted to support program syntax that makes use of the “future reserved words” listed in 7.5.3 of this specification.

3 References

ISO/IEC 9899:1996 Programming Languages – C, including amendment 1 and technical corrigenda 1 and 2. ISO/IEC 10646-1:1993 Information Technology -- Universal Multiple-Octet Coded Character Set (UCS) plus its amendments and corrigenda.

Unicode Inc. (1996), The Unicode Standard, Version 2.0. ISBN: 0-201-48345-9, Addison-Wesley Publishing Co., Menlo Park, California.

Unicode Inc. (1998), Unicode Technical Report #8: The Unicode Standard, Version 2.1. Unicode Inc. (1998), Unicode Technical Report #15: Unicode Normalization Forms. ANSI/IEEE Std 754-1985: IEEE Standard for Binary Floating-Point Arithmetic. Institute of Electrical and Electronic Engineers, New York (1985).

4 Overview

This section contains a non-normative overview of the ECMAScript language. ECMAScript is an object-oriented programming language for performing computations and manipulating computational objects within a host environment. ECMAScript as defined here is not intended to be computationally self-sufficient; indeed, there are no provisions in this specification for input of external data or output of computed results. Instead, it is expected that the computational environment of an ECMAScript program will provide not only the objects and other facilities described in this specification but also certain environment-specific host objects, whose description and behaviour are beyond the scope of this specification except to indicate that they may provide certain properties that can be accessed and certain functions that can be called from an ECMAScript program. A scripting language is a programming language that is used to manipulate, customise, and automate the facilities of an existing system. In such systems, useful functionality is already available through a user interface, and the scripting language is a mechanism for exposing that functionality to program control. In this way, the existing system is said to provide a host environment of objects and facilities, which completes the capabilities of the scripting language. A scripting language is intended for use by both professional and non- professional programmers. To accommodate non-professional programmers, some aspects of the language may be somewhat less strict.

ECMAScript was originally designed to be a Web scripting language , providing a mechanism to enliven Web pages in browsers and to perform server computation as part of a Web-based client-server architecture. ECMAScript can provide core scripting capabilities for a variety of host environments, and therefore the core scripting language is specified in this document apart from any particular host environment. Some of the facilities of ECMAScript are similar to those used in other programming languages; in particular Java¥ and Self, as described in:

  • Gosling, James, Bill Joy and Guy Steele. The Java^ Language Specification. Addison Wesley Publishing Co., 1996.
  • Ungar, David, and Smith, Randall B. Self: The Power of Simplicity. OOPSLA '87 Conference Proceedings, pp. 227–241, Orlando, FL, October 1987.

4.1 Web Scripting

A web browser provides an ECMAScript host environment for client-side computation including, for instance, objects that represent windows, menus, pop-ups, dialog boxes, text areas, anchors, frames, history, cookies, and input/output. Further, the host environment provides a means to attach scripting code to events such as change of focus, page and image loading, unloading, error and abort, selection, form submission, and mouse actions. Scripting code appears within the HTML and the displayed page is a combination of user interface elements and fixed and computed text and images. The scripting code is reactive to user interaction and there is no need for a main program. A web server provides a different host environment for server-side computation including objects representing requests, clients, and files; and mechanisms to lock and share data. By using browser-side and server-side scripting together, it is possible to distribute computation between the client and server while providing a customised user interface for a Web-based application. Each Web browser and server that supports ECMAScript supplies its own host environment, completing the ECMAScript execution environment.

4.2 Language Overview

The following is an informal overview of ECMAScript—not all parts of the language are described. This overview is not part of the standard proper. ECMAScript is object-based: basic language and host facilities are provided by objects, and an ECMAScript program is a cluster of communicating objects. An ECMAScript object is an unordered collection of properties each with zero or more attributes that determine how each property can be used— for example, when the ReadOnly attribute for a property is set to true , any attempt by executed ECMAScript code to change the value of the property has no effect. Properties are containers that hold other objects, primitive values , or methods. A primitive value is a member of one of the following built-in types: Undefined , Null , Boolean , Number , and String ; an object is a member of the remaining built-in type Object ; and a method is a function associated with an object via a property. ECMAScript defines a collection of built-in objects that round out the definition of ECMAScript entities. These built-in objects include the Global object, the Object object, the Function object, the Array object, the String object, the Boolean object, the Number object, the Math object, the Date object, the RegExp object and the Error objects Error, EvalError , RangeError, ReferenceError, SyntaxError, TypeError and URIError. ECMAScript also defines a set of built-in operators that may not be, strictly speaking, functions or methods. ECMAScript operators include various unary operations, multiplicative operators, additive operators, bitwise shift operators, relational operators, equality operators, binary bitwise operators, binary logical operators, assignment operators, and the comma operator. ECMAScript syntax intentionally resembles Java syntax. ECMAScript syntax is relaxed to enable it to serve as an easy-to-use scripting language. For example, a variable is not required to have its type declared nor are types associated with properties, and defined functions are not required to have their declarations appear textually before calls to them.

4.3 Definitions

The following are informal definitions of key terms associated with ECMAScript. 4.3.1 Type A type is a set of data values. 4.3.2 Primitive Value A primitive value is a member of one of the types Undefined , Null , Boolean , Number , or String. A primitive value is a datum that is represented directly at the lowest level of the language implementation. 4.3.3 Object An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitive value, object, or function. A function stored in a property of an object is called a method. 4.3.4 Constructor A constructor is a Function object that creates and initialises objects. Each constructor has an associated prototype object that is used to implement inheritance and shared properties. 4.3.5 Prototype A prototype is an object used to implement structure, state, and behaviour inheritance in ECMAScript. When a constructor creates an object, that object implicitly references the constructor’s associated prototype for the purpose of resolving property references. The constructor’s associated prototype can be referenced by the program expression constructor .prototype, and properties added to an object’s prototype are shared, through inheritance, by all objects sharing the prototype. 4.3.6 Native Object A native object is any object supplied by an ECMAScript implementation independent of the host environment. Standard native objects are defined in this specification. Some native objects are built-in; others may be constructed during the course of execution of an ECMAScript program. 4.3.7 Built-in Object A built-in object is any object supplied by an ECMAScript implementation, independent of the host environment, which is present at the start of the execution of an ECMAScript program. Standard built-in objects are defined in this specification, and an ECMAScript implementation may specify and define others. Every built-in object is a native object. 4.3.8 Host Object A host object is any object supplied by the host environment to complete the execution environment of ECMAScript. Any object that is not native is a host object. 4.3.9 Undefined Value The undefined value is a primitive value used when a variable has not been assigned a value. 4.3.10 Undefined Type The type Undefined has exactly one value, called undefined. 4.3.11 Null Value The null value is a primitive value that represents the null, empty, or non-existent reference. 4.3.12 Null Type The type Null has exactly one value, called null. 4.3.13 Boolean Value A boolean value is a member of the type Boolean and is one of two unique values, true and false. 4.3.14 Boolean Type The type Boolean represents a logical entity and consists of exactly two unique values. One is called true and the other is called false.

4.3.15 Boolean Object

A Boolean object is a member of the type Object and is an instance of the built-in Boolean object. That is, a Boolean object is created by using the Boolean constructor in a new expression, supplying a boolean as an argument. The resulting object has an implicit (unnamed) property that is the boolean. A Boolean object can be coerced to a boolean value.

4.3.16 String Value

A string value is a member of the type String and is a finite ordered sequence of zero or more 16-bit unsigned integer values. NOTE Although each value usually represents a single 16-bit unit of UTF-16 text, the language does not place any restrictions or requirements on the values except that they be 16-bit unsigned integers.

4.3.17 String Type

The type String is the set of all string values.

4.3.18 String Object

A String object is a member of the type Object and is an instance of the built-in String object. That is, a String object is created by using the String constructor in a new expression, supplying a string as an argument. The resulting object has an implicit (unnamed) property that is the string. A String object can be coerced to a string value by calling the String constructor as a function (15.5.1).

4.3.19 Number Value

A number value is a member of the type Number and is a direct representation of a number.

4.3.20 Number Type

The type Number is a set of values representing numbers. In ECMAScript, the set of values represents the double-precision 64-bit format IEEE 754 values including the special “Not-a-Number” (NaN) values, positive infinity, and negative infinity.

4.3.21 Number Object

A Number object is a member of the type Object and is an instance of the built-in Number object. That is, a Number object is created by using the Number constructor in a new expression, supplying a number as an argument. The resulting object has an implicit (unnamed) property that is the number. A Number object can be coerced to a number value by calling the Number constructor as a function (15.7.1).

4.3.22 Infinity

The primitive value Infinity represents the positive infinite number value. This value is a member of the Number type.

4.3.23 NaN

The primitive value NaN represents the set of IEEE Standard “Not-a-Number” values. This value is a member of the Number type.

Productions of the syntactic grammar are distinguished by having just one colon “ : ” as punctuation.

The syntactic grammar as presented in sections 0, 0, 0 and 0 is actually not a complete account of which token sequences are accepted as correct ECMAScript programs. Certain additional token sequences are also accepted, namely, those that would be described by the grammar if only semicolons were added to the sequence in certain places (such as before line terminator characters). Furthermore, certain token sequences that are described by the grammar are not considered acceptable if a terminator character appears in certain “awkward” places.

5.1.5 Grammar Notation

Terminal symbols of the lexical and string grammars, and some of the terminal symbols of the syntactic grammar, are shown in fixed width font, both in the productions of the grammars and throughout this specification whenever the text directly refers to such a terminal symbol. These are to appear in a program exactly as written. All nonterminal characters specified in this way are to be understood as the appropriate Unicode character from the ASCII range, as opposed to any similar-looking characters from other Unicode ranges. Nonterminal symbols are shown in italic type. The definition of a nonterminal is introduced by the name of the nonterminal being defined followed by one or more colons. (The number of colons indicates to which grammar the production belongs.) One or more alternative right-hand sides for the nonterminal then follow on succeeding lines. For example, the syntactic definition: WithStatement : with ( Expression ) Statement

states that the nonterminal WithStatement represents the token with , followed by a left parenthesis token, followed by an Expression , followed by a right parenthesis token, followed by a Statement. The occurrences of Expression and Statement are themselves nonterminals. As another example, the syntactic definition: ArgumentList : AssignmentExpression ArgumentList , AssignmentExpression states that an ArgumentList may represent either a single AssignmentExpression or an ArgumentList , followed by a comma, followed by an AssignmentExpression. This definition of ArgumentList is recursive , that is, it is defined in terms of itself. The result is that an ArgumentList may contain any positive number of arguments, separated by commas, where each argument expression is an AssignmentExpression. Such recursive definitions of nonterminals are common. The subscripted suffix “ opt ”, which may appear after a terminal or nonterminal, indicates an optional symbol. The alternative containing the optional symbol actually specifies two right-hand sides, one that omits the optional element and one that includes it. This means that: VariableDeclaration : Identifier Initialiseropt is a convenient abbreviation for: VariableDeclaration : Identifier Identifier Initialiser and that: IterationStatement : for ( ExpressionNoIn (^) opt ; Expression (^) opt ; Expression (^) opt ) Statement is a convenient abbreviation for: IterationStatement : for ( ; Expression (^) opt ; Expression (^) opt ) Statement for ( ExpressionNoIn ; Expression (^) opt ; Expression (^) opt ) Statement

which in turn is an abbreviation for:

IterationStatement : for ( ; ; Expression (^) opt ) Statement for ( ; Expression ; Expression (^) opt ) Statement for ( ExpressionNoIn ; ; Expression (^) opt ) Statement for ( ExpressionNoIn ; Expression ; Expression (^) opt ) Statement

which in turn is an abbreviation for:

IterationStatement : for ( ; ; ) Statement for ( ; ; Expression ) Statement for ( ; Expression ; ) Statement for ( ; Expression ; Expression ) Statement for ( ExpressionNoIn ; ; ) Statement for ( ExpressionNoIn ; ; Expression ) Statement for ( ExpressionNoIn ; Expression ; ) Statement for ( ExpressionNoIn ; Expression ; Expression ) Statement

so the nonterminal IterationStatement actually has eight alternative right-hand sides. If the phrase “ [empty] ” appears as the right-hand side of a production, it indicates that the production's right-hand side contains no terminals or nonterminals. If the phrase “ [lookahead ∉ set ] ” appears in the right-hand side of a production, it indicates that the production may not be used if the immediately following input terminal is a member of the given set. The set can be written as a list of terminals enclosed in curly braces. For convenience, the set can also be written as a nonterminal, in which case it represents the set of all terminals to which that nonterminal could expand. For example, given the definitions

DecimalDigit :: one of 0 1 2 3 4 5 6 7 8 9

DecimalDigits :: DecimalDigit DecimalDigits DecimalDigit

the definition

LookaheadExample :: n [lookahead ∉ { 1 , 3 , 5 , 7 , 9 }] DecimalDigits DecimalDigit [lookahead ∉ DecimalDigit ]

matches either the letter n followed by one or more decimal digits the first of which is even, or a decimal digit not followed by another decimal digit. If the phrase “ [no LineTerminator here] ” appears in the right-hand side of a production of the syntactic grammar, it indicates that the production is a restricted production : it may not be used if a LineTerminator occurs in the input stream at the indicated position. For example, the production:

ReturnStatement : return [no LineTerminator here] Expression (^) opt ;

indicates that the production may not be used if a LineTerminator occurs in the program between the return token and the Expression. Unless the presence of a LineTerminator is forbidden by a restricted production, any number of occurrences of LineTerminator may appear between any two consecutive tokens in the stream of input elements without affecting the syntactic acceptability of the program.