ECMAScript from specification perspective is a series of articles and notes that I will write to look at various ECMAScript behaviors (features or exceptions) from the bottom of the specification.

All the articles are hereGithubWelcome to Star and Watch.

The reference specification is ECMASCript2018 (ES9) at 🔗.

ES9 is officially the latest version of ES, released in June 2018. ECMAScript2019🔗 is currently in draft (about 19.6). It won’t change much, but the stable version will prevail.

ES historical version address 🔗, ES latest version address 🔗.

This article also referred to ZhoushengmuFC’s Chinese version ES6. I also recommend a Github project: Engine262 🔗. This project implements a standard ES interpreter according to the specification. (It is a real standard, which basically fully conforms to the algorithm steps in the specification. Debugging is more helpful for understanding the runtime semantics of the specification.)

The author’s article is mainly the analysis of ES runtime semantics, so there is not much about its static semantics.

If you want to read the spec in its entirety, you can read ES by a great engineer who also contributed to the aforementioned engine262.


This paper mainly introduces the specification 1~5 chapters, is the general overview of ES and specification of the notation convention.

introduce

ES1th was released in June 1997 and ES2th was released in June 1998, mainly as a format revision of the first version.

ES3th introduces regular expressions, better string handling, new control statements, try/catch exception handling, stricter error definition, and format for digital output. Released in December 1999.

After ES3, ES was adopted by the World Wide Web and supported by most browsers. But ES4th was not finished because of disagreements about the complexity of language.

ES5th added features such as strict schemas, accessor properties, JSON support, and more, and was released in December 2009. ES5.1, a minor modification of ES5, was released in June 2011.

ES6th was released in June 2015. In fact, the intensive development of ES6 began in 2009, and with a lot of experimentation and language enhancement design work before the third edition was published in 1999, IT can be said that ES6 is the result of 15 years of hard work. Development goals include better support for large applications, library creation, and the use of ECMAScript as a compilation target for other languages.

The main improvements are: modules, class declarations, lexical scope, iterators and generators, promise asynchronous programming, structural patterns, tail calls. Extends the ECMAScript built-in library to support additional data abstractions, including maps, sets, binary numeric arrays, and additional support for Unicode complementary characters in strings and regular expressions.

ES7th adds index manipulation, array.prototype.includes. In addition, from ES7 onwards, ES has been released every year (I guess 😂).

ES8th introduces Async, shared memory, Atomics, library enhancements, Bug fixes.

Async functions enhance the asynchronous programming experience by providing the Promise-RETURNING function.

Shared memory and Atomics introduce a new memory model that allows multiple agents to communicate using atomic operations, ensuring well-defined execution orders even on parallel processors. (Don’t understand at the moment)

Also added the Object values, Object. Entries, and the Object. GetOwnPropertyDescriptors.

This specification (ES9th) introduces support for asynchronous iteration through the asynchronous iterator protocol and asynchronous generators. Four new regular expression features were introduced, as well as rest parameter and extended operator support for object attributes.

An overview of the

ECMAScript is an object-oriented programming language for performing computations and manipulating computed objects in a host environment.

ECMAScript was originally designed as a Web scripting language, providing a mechanism to make Web pages more interesting in the browser and perform server calculations as part of a Web-based client-server architecture.

ECMAScript is now used to provide core scripting functionality for a variety of hosting environments.


That’s all that’s useful in this section, where the host environment refers to the browser or Node environment, etc.

Web Scripting

The browser

Web browsers provide ECMAScript hosting environments for client calculations, such as window objects, menus, pop-up Windows, dialogs, text areas, links, frames, browsing history, cookies, and INPUT/output.

In addition, the host environment provides a way to attach script code to events, such as focus changes, mouse actions, and so on. Script code responds to user interactions without the need for a main program.

ES on the browser side is designed to handle the interaction between the user and the browser.

The service side

Web servers provide a different hosting environment for server-side computing, including requests, file IO, and so on.

Each ECMAScript enabled Web browser and server provides its own hosting environment to complete the ECMAScript execution environment.

ECMAScript overview

ES is object based, the basic language and host tools are provided by objects, ES program is a set of communication objects.

Object is a collection of properties. The attribute of each attribute determines the attribute behavior. For example, its writable signals determine whether it is writable and can be configured without any additional control.

A property is a container for other objects, primitive values, and functions.

There are six types of primitive values: Undefined, Null, Boolean, Number, String and Symbol.

Functions are callable objects. The functions associated with an object through attributes are called methods.

ECMAScript defines a set of built-in objects that complete the definition of ECMAScript entities.

These built-in objects have

Global objects;

Objects that maintain the basics of language runtime semantics: Object, Function, Boolean, Symbol, and various Error objects;

Objects that represent and manipulate numerical values: Math, Number, and Date;

For literal objects: String and RegExp;

An index collection of values: Array, and nine types of arrays whose elements have a specific numeric data representation;

Key Set objects: Map, Set;

Structured data objects: JSON, ArrayBuffer, SharedArrayBuffer, and DataView;

Controlling abstract objects: generator functions, promises;

Reflect objects: Proxy and Reflect.

ECMAScript defines a number of built-in operators: * / + – >> and so on.

Large ES programs support modulization, which allows programs to be divided into multiple statements and declarative sequences. Each module explicitly identifies the declarations it uses that need to be provided by other modules, and which of its declarations are available to other modules.


This section gives you an overview of the type of ES, built-in objects. These types will be discussed in detail in Chapter 6.

object

ES objects are not entirely class-based. Objects can be created and assigned attributes through various methods, such as literal definitions or constructors.

Each constructor is a function with a stereotype property for prototype-based inheritance and property sharing.

The object created by the constructor has an implicit reference, called the object prototype, to the constructor’s Prototype property.

A stereotype can have a non-null implicit reference to its stereotype, which is called a stereotype chain.

When referring to an attribute in an object, the reference refers to the attribute of that name in the first object in the stereotype chain that contains the attribute of that name. First examine this property of the immediate object; If the object contains a named attribute, that attribute is referenced; If the object does not contain named attributes, then the prototype of the object is examined.

(I’m not going to explain the above picture, which you should understand. If you don’t understand it, this article may not be suitable for you.

In class-based object-oriented languages, state is typically carried by instances, methods are carried by classes, and inheritance has only structure and behavior. In ECMAScript, states and methods are hosted by objects, while structure, behavior, and state are inherited.

Strict mode

ES also provides strict schemas, strict variants that exclude certain syntactic and semantic features of the regular ECMAScript language and modify the detailed semantics of some features.

Briefly described, details 🔗

Terms and Definitions

This section defines a number of terms, such as types, stereotypes, primitive values, constructors, and so on, as well as the range of values such as string, number, and so on. The details are 🔗.

To give you a few confusing terms, there are four types of objects:

  • ordinary objectOrdinary objectsThe: object has basic internal methods supported by all objects.
  • exotic objectStrange objects: An object without one or more basic internal default methods. (Any object that is not an ordinary object is an ordinary object.)
  • standard objectStandard object: Its semantics are defined by this specification. A standard object can be a normal object or a weird object.
  • built-in objectBuilt-in objects: Objects implemented according to ECMAScript. Standard built-in objects are defined in this specification. ECMAScript implementations (e.g. JS, Node) can specify and provide additional classes of built-in objects.Built-in constructorIs a built-in object that is also a constructor. All standard objects are built-in objects.

For example:

// Ordinary objects are ordinary objects
let ordinary_obj = {
  a: 'test'.b: 1
}
// Weird objects are non-ordinary objects, such as function parameters args, array objects, string objects, etc
let standard_object = new String('lalala')
// Standard objects are defined by this specification for example
let standard_obj = new Array(a)// Built-in objects are objects built into the host environment for example
let built_in_obj = console
Copy the code

These are not too important, I will not go into details (because I do not understand 😂)

This specification is organized in sections

Chapter five: Standard notation agreement.

Chapter 6-9: defines the execution environment of ES program.

Chapter 10-16: defines the actual ES programming language, including syntactic coding, runtime semantics, and so on.

Chapter 17-26 defines the ES standard library, including all the standard objects available for the execution of ES programs.

Chapter 27 describes the memory consistency model of memory access supported by SharedArrayBuffer and the method of Atomics objects.

Notation convention

This chapter is mainly about some compilation principles. Without further discussion, you can visit 🔗 if you are interested.

Grammar and morphology

Context free method

A context-free grammar consists of multiple productions. Each production has a non-terminal symbol on its left and zero or more non-terminal symbols and terminals on its right. For each grammar, the terminal symbol is drawn from the specified alphabet.

A chain production is a production with one non-terminal symbol and zero or more terminal symbols on its right side.

Starting with a sentence consisting of a single differentiated non-terminator (called the target symbol), a given context-free grammar specifies the language, that is, a possible sequence in which any non-terminator in the sequence on the right can be repeatedly replaced by a sequence in which the non-terminator is the left terminal symbol.

Let’s see: Chestnut:

ArrayLiteral: ​ [Elision]

​ [ElementList]

​ [ElementList, Elision] ElementList: ​ Elision AssignmentExpression

​ Elision SpreadElement

​ ElementList, Elision AssignmentExpression

​ ElementList, Elision SpreadElement Elision: ​ ,

Elision , SpreadElement: … AssignmentExpression

Above is the generation that defines array literals (omitting many arguments, adding arguments to make syntax more complete) 🔗. The italics are non-terminal, which means you can go further; Non-italics are terminators, meaning no further derivation.

From top to bottom, ArrayLiteral represents an ArrayLiteral, which can be represented in three possible ways, all of which can be pushed down until no further derivation is possible.

For example, [] belongs to [Elision] and [1,2] belongs to [ElementList].

This description overrides the syntactic form of all array literals in ES.

Algorithm agreed

In describing the semantics of the ES language, some algorithmic steps are used, which you can think of as pseudo-code.

Very simple (false 😂, some I also do not quite understand, dare not disorderly speak. But it doesn’t affect understanding run-time semantics, so I won’t say more.)


This article is over, feel did not say what, a lot of I do not care about did not say, a little ashamed. 😂

The next article will begin in a formal way with ES data types and values (both linguistic and canonical) and some basic abstraction operations (used to describe the semantics of ES).