ECMAScript: an introduction

The best definition of ECMAScript is given in the Overview of the language itself:

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.

ECMAScript forms the core of any JavaScript implementation. In its basic form, ECMAScript is more an object-based language than a object-oriented one (like Java, for example). We'll discuss this aspect later. We must distinguish between the ECMAScript implementation and the DOM implementation in a web browser: while JavaScript implementation is based on ECMAScript, the DOM implementation is based on the set of interfaces defined in the W3C DOM specifications. Both implementations are handled by two separate engines; that's why DOM manipulation is more expensive than simple JavaScript routines, because when you invoke for example the getElementsByTagName() method , you're actually using the DOM engine, but when you use String.prototype you're using the ECMAScript engine instead.

ECMAScript has no concept of classes, which are a basic concept in traditional OO languages. Although ECMAScript includes the keyword class among its future reserved words, it's more appropriate to use the term object either when we're talking about native ECMAScript objects (such as Number, String and so on) or user-defined ones. That's why we should consider ECMAScript as an object-based language. As the specifications say:

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 a collection of properties each with zero or more attributes that determine how each property can be used—for example, when the Writable attribute for a property is set to false, any attempt by executed ECMAScript code to change the value of the property fails. Properties are containers that hold other objects, primitive values, or functions. 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 function is a callable object. A function that is associated with an object via a property is a method. 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, the JSON object, and the Error objects Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError and URIError.

ECMAScript also defines a set of built-in operators. 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.

ECMAScript doesn't handle objects and inheritance through the traditional OO paradigm. In a classical OO language, you may have:

class A {
    public property;
}

class B extends A {
    public function getProperty {
        return this.property;
    }
}

Instead. in the ECMAScript specification it is stated that:

ECMAScript does not use classes such as those in C++, Smalltalk, or Java. Instead objects may be created in various ways including via a literal notation or via constructors which create objects and then execute code that initialises all or part of them by assigning initial values to their properties. Each constructor is a function that has a property named ―prototype‖ that is used to implement prototype-based inheritance and shared properties. Objects are created by using constructors in new expressions; for example, new Date(2009,11) creates a new Date object. Invoking a constructor without using new has consequences that depend on the constructor. For example, Date() produces a string representation of the current date and time rather than an object.

Every object created by a constructor has an implicit reference (called the object‘s prototype) to the value of its constructor‘s ―prototype‖ property. Furthermore, a prototype 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.

So you may have:

function A(property) {
    this.property = property;
}

B.prototype = new A();
B.prototype.constructor = B;

function B(property) {
   this.property = property;
}

B.prototype.getProperty = function() {

    return 'B property is' + this.property;

};

But ECMAScript is also a scripting language. It is said that:

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.

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.

But what is web scripting? Here's the answer:

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.

Now ECMAScript is used in many different contexts. It can be used as the core of languages such as ActionScript, or even to form one of the components of an API. I've used it recently on SVG documents, for example. This demonstrates how ECMAScript can actually be extended and interact with other web standards.

Leave a Reply

Note: Only a member of this blog may post a comment.