
JavaScript 2.0: Evolving a Language for Evolving Systems Waldemar Horwat [email protected] Abstract JavaScript 2.0 is the next major revision of the JavaScript language. Also known as ECMAScript Edition 4, it is being standardized by the ECMA organization. This paper summarizes the needs that drove the revision in the language and then describes some of the major new features of the language to meet those needs — support for API evolution, classes, packages, object protection, dynamic types, and scoping. JavaScript is a very widely used language, and evolving it presented many unique challenges as well as some opportunities. The emphasis is on the rationale, insights, and constraints that led to the features rather than trying to describe the complete language. Netscape browsers through an interface 1 Introduction called LiveConnect. JavaScript as a language has computational 1.1 Background facilities only — there are no input/output JavaScript [6][8] is a web scripting language primitives defined within the language. In- invented by Brendan Eich at Netscape. This stead, each embedding of JavaScript within language first appeared in 1996 as a particular environment provides the means JavaScript 1.0 in Navigator 2.0. Since then to interact with that environment. Within a the language has undergone numerous addi- web browser JavaScript is used in conjunc- tions and revisions [6], and the most recent tion with a set of common interfaces, in- released version is JavaScript 1.5. cluding the Document Object Model [11], which allow JavaScript programs to interact JavaScript has been enormously successful with web pages and the user. These inter- — it is more than an order of magnitude faces are described by separate standards more widely used than all other web client and are not part of the JavaScript language languages combined. More than 25% of web itself. This paper concentrates on the pages use JavaScript. JavaScript language rather than the inter- faces. JavaScript programs are distributed in source form, often embedded inside web page elements, thus making it easy to author 1.2 Standardization them without any tools other than a text After Netscape released JavaScript in Navi- editor. This also makes it easier to learn the gator 2.0, Microsoft implemented the lan- language by examining existing web pages. guage, calling it JScript, in Internet Ex- plorer 3.0. Netscape, Microsoft, and a num- There is a plethora of synonymous names ber of other companies got together and for JavaScript. JavaScript, JScript, and formed the TC39 committee in the ECMA ECMAScript are all the same language. JavaScript was originally called LiveScript standards organization [2] in order to agree on a common definition of the language. but was renamed to JavaScript just before it The first ECMA standard [3], calling the was released. JavaScript is not related to Java, although the two language implemen- language ECMAScript, was adopted by the ECMA general assembly in June 1997 as the tations can communicate with each other in ECMA-262 standard. The second edition of JavaScript 2.0: Evolving a Language for Evolving Systems 1 this standard, ECMA-262 Edition 2 [4], con- attributes and conditional compilation (Sec- sisted mainly of editorial fixes gathered in tion 8). Section 9 concludes. the process of making the ECMAScript ISO standard 16262. The third edition of the ECMAScript standard [5] was adopted in 2 JavaScript 1.5 December 1999 and added numerous new JavaScript 1.5 (ECMAScript Edition 3) is an features, including regular expressions, object-based scripting language with a syn- nested functions and closures, array and ob- tax similar to C and Java. Statements such as ject literals, the and - switch do while if, while, for, switch, and statements, and exceptions. JavaScript 1.5 throw/try/catch will be familiar to fully implements ECMAScript Edition 3. C/C++ or Java programmers. Functions, I’ve been involved at Netscape with both the declared using the function keyword, can implementation and standardization of nest and form true closures. For example, JavaScript since 1998. I wrote parts of the given the definitions ECMAScript Edition 3 standard and am cur- function square(x) { rently the editor of the draft ECMAScript return x*x; Edition 4 standard. } In Editions 1 and 2, the ECMA committee function add(a) { standardized existing practice, as the lan- return function(b) { guage had already been implemented by return a+b; Netscape, and Microsoft closely mirrored } that implementation. In Edition 3, the role of } the committee shifted to become more active evaluating the expressions below produces in the definition of new language features the values listed after the fi symbols: before they were implemented by the ven- square(5) fi 25 dors; without this approach, the vendors’ var f = add(3); implementations would have quickly di- var g = add(6); verged. This role continues with Edition 4, f(1) fi 4; and, as a result, the interesting language de- g(5) fi 11; sign discussions take place mainly within the ECMA TC39 (now TC39TG1) working A function without a return statement group. returns the value undefined. This paper presents the results of a few of Like Lisp, JavaScript provides an eval these discussions. Although many of the is- function that takes a string and compiles and sues have been settled, Edition 4 has not yet evaluates it as a JavaScript program; this been approved or even specified in every allows self-constructing and self-modifying detail. It is still likely to change and should code. For example: definitely be considered a preliminary draft. eval("square(8)+3") fi 67 eval("square = f") fi The 1.3 Outline source code for function f Section 2 gives a brief description of the square(2) fi 5 existing language JavaScript 1.5. Section 3 summarizes the motivation behind Java- 2.1 Values and Variables Script 2.0. Individual areas and decisions are covered in subsequent sections: types (Sec- The basic values of JavaScript 1.5 are num- tion 4); scoping and syntax issues (Sec- bers (double-precision IEEE floating-point tion 5); classes (Section 6); namespaces, values including +0.0, –0.0, +∞, –∞, and versioning, and packages (Section 7); and NaN), booleans (true and false), the JavaScript 2.0: Evolving a Language for Evolving Systems 2 special values null and undefined, im- strange("Apple ", false) fi mutable Unicode strings, and general ob- "Apple Hello" jects, which include arrays, regular expres- strange(20, true) fi sions, dates, functions, and user-defined ob- "40Hello" jects. All values have unlimited lifetime and The last example also shows that + is poly- are deleted only via garbage collection, morphic — it adds numbers, concatenates which is transparent to the programmer. strings, and, when given a string and a num- ber, converts the number to a string and Variables are not statically typed and can concatenates it with the other string. hold any value. Variables are introduced using var declarations as in: var x; 2.2 Objects var y = z+5; JavaScript 1.5 does not have classes; in- stead, general objects use a prototype An uninitialized variable gets the value mechanism to mimic inheritance. Every ob- undefined. Variable declarations are ject is a collection of name-value pairs lexically scoped, but only at function called properties, as well as a few special, boundaries — all declarations directly hidden properties. One of the hidden prop- within a function apply to the entire func- erties is a prototype link1 which points to tion, even above the point of declaration. another object or null. Local blocks do not form scopes. If a func- tion accesses an undeclared variable, it is When reading property p of object x using assumed to be a global variable. For exam- the expression x.p, the object x is searched ple, in the definitions first for a property named p. If there is one, function init(a) { its value is returned; if not, x’s prototype b = a; (let’s call it y) is searched for a property } named p. If there isn’t one, y’s prototype is searched next and so on. If no property at all function strange(s, t) { is found, the result is the value a = s; undefined. if (t) { var a; When writing property p of object x using a = a+a; the expression x.p = v, a property named p } is created in x if it’s not there already and return a+b; then assigned the value v. x’s prototype is } not affected by the assignment. The new function strange defines a local variable property p in x will then shadow any prop- a. It doesn’t matter that the var statement is erty with the same name in x’s prototype and nested within the if statement — the var can only be removed using the expression statement creates a at the beginning of the delete x.p. function regardless of the value of t. A property can be read or written using an At this point evaluating indirect name with the syntax x[s], where s strange("Apple ", false) is an expression that evaluates to a string (or signals an error because the global variable a value that can be converted into a string) b is not defined. However, the following representing a property name. If s contains statements evaluate successfully because the string "blue", then the expression init creates the global variable b: x[s] is equivalent to x.blue. An array is init("Hello") fi undefined 1 For historical reasons in Netscape’s JavaScript this hidden prototype link is accessible as the property named __proto__, but this is not part of the ECMA standard. JavaScript 2.0: Evolving a Language for Evolving Systems 3 an object with properties named "0", "1", method can refer to the object on which it "2", …, "576", etc.; not all of these need was invoked using the this variable: be present, so arrays are naturally sparse.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages15 Page
-
File Size-