4 Overview
EMCAScript 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 behavior 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, customize, 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, and therefore there may be a number of informalities and built into the language.
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.
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 customized 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 0 or more attributes which 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 which 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, and the Date object.
ECMAScript also defines a set of built-in operators which 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, anddefined functions are not required to have their declarations appear textually before calls to them
4.2.1 Objects
ECMAScript does not contain proper classes such as those in C++, Smalltalk, or Java, but rather, supports constructors which create objects by executing code that allocates storage for the objects and initializes all or part of them by assigning initial values to their properties. All functions including constructors are objects, but not all objects are constructors. Each constructor has a Prototype property which is used to implement prototype-based inheritance and shared properties . Objects are created by using constructors in new expressions, for example, new String("A String") creates a new string object. Invoking a constructor without using new has consequences that depend on the constructor. For example, String("A String") produces a primitive string, not an object.
ECMAScript supports prototype-based inheritance . Every constructor has an associated prototype, and every object created by that constructor has an implicit reference to the prototype (called the object's prototype ) associated with its constructor. Furthermore, aprototype may have a non-null implicit reference to its prototype, and so on; this is called the prototype chain . When a reference is made to a property in an object, that reference is to the property of that name in the first object in the prototype chain that contains a property of that name. In
other words, first the object mentioned directly is examined for such a property; if that object contains the named property, that is the property to which the reference refers; if that object does not contain the named property, the prototype for that object is examined next; and so on.
In a class-based object-oriented language, in general, state is carried by instances, methods are carried by classes, and inheritance is only of structure and behavior. In ECMAScript, the state and methods are carried by objects, and structure, behavior, and state are all inherited.
All objects that do not directly contain a particular property that their prototype contains share that property and its value. The following diagram may illustrate this discussion:
CF is a constructor (and also an object). Five objects have been created by using new expressions: cf1 , cf2 , cf3 , cf4 , and cf5 .Each of these objects contains properties named q1 and q2. The dashed lines represent the implicit prototype relationship; so, for example, cf3 's prototype is CFp . The constructor, CF , has two properties itself, named P1 and P2, which are not visible to CFp , cf1 , cf2 , cf3 , cf4 , or cf5 . The property named CFP1 in CFp is shared by: cf1 , cf2 , cf3 , cf4 , and cf5 , as are any properties found in CFp 's implicit prototype chain which are not named q1, q2, or CFP1. Notice that there is no implicit prototype link between CFp and CF .
Unlike class-based object languages, properties can be added to objects on the fly simply by assigning values to them. That is, constructors are not required to name or assign values to all or any of its properties. In the above diagram, one could add a new shared property for cf1 , cf2 , cf3 , cf4 , and cf5 by assigning a new value to the property in CFp .
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. In general, the correct functioning of a program is not affected if different data values of the same type are substituted for others.
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 which 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 which contain primitive values, objects, or functions. A function stored in the property of an object is called a method.
4.3.4 Constructor
A constructor is a function object which creates and initializes objects. Each constructor has an associated prototype object which is used to implement inheritance and shared properties.
4.3.5 Prototype
A prototype is an object used to implement structure, state, and behavior 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, that is present at the start of the execution of an ECMAScript program. Standard built-in objects are defined in this specification, and the 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
Undefined 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
Null is a primitive value that represents the null, empty, or nonexistent 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 either 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 Boolean object which is a constructor. 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 which is the boolean. A boolean object can be coerced to a boolean value. A boolean object can be used anywhere a boolean value is expected.
This is an example of one of the conveniences built into ECMAScript—in this case it is to accommodate programmers of varying backgrounds. Those familiar with imperative or procedural programming languages may find number values more natural, while those familiar with object-oriented languages may find number objects more intuitive.
4.3.16 String value
A string value is a member of the type String and is the set of all finite ordered sequences of zero or more Unicode characters.
4.3.17 String type
The type String is the set of all finite ordered sequences of zero or more Unicode characters.
4.3.18 String object
A string object is a member of the type Object and is an instance of the String object which is a constructor. 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 which is the string. A string object can be coerced to a string value. A string object can be used anywhere a string value is expected.
4.3.19 Number value
A number value 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 represent the double-precision 64-bit format IEEE 754 value along with a special "Not-a-Number" (NaN) value, 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 Number object which is a constructor. 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 which is the number. A number object can be coerced to a number value. A number object can be used anywhere a number value is expected. Note that a number object can have shared properties by adding them to the Number prototype.
4.3.22 Infinity
The primitive value Infinity represents the positive infinite number value.
4.3.23 NaN
The primitive value NaN represents the set of IEEE Standard "Not-a-Number" values.