Previous Contents Index Next
Core JavaScript Guide 1.5

> Chapter 1 JavaScript Overview
> This chapter introduces JavaScript, discusses some of its fundamental concepts, and describes the new features in the 1.5 release.

> This chapter contains the following sections:

> What Is JavaScript?

> - JavaScript and Java

> - JavaScript and the ECMA Specification

> - New Features in this Release

> What Is JavaScript?

> JavaScript is Netscape's cross-platform, object-oriented scripting language. JavaScript is a small, lightweight language; it is not useful as a standalone language, but is designed for easy embedding in other products and applications, such as web browsers. Inside a host environment, JavaScript can be connected to the objects of its environment to provide programmatic control over them.

> Core JavaScript contains a core set of objects, such as Array, Date, and Math, and a core set of language elements such as operators, control structures, and statements. Core JavaScript can be extended for a variety of purposes by supplementing it with additional objects; for example:

> Client-side JavaScript extends the core language by supplying objects to control a browser (Navigator or another web browser) and its Document Object Model (DOM). For example, client-side extensions allow an application to place elements on an HTML form and respond to user events such as mouse clicks, form input, and page navigation.

> - Server-side JavaScript extends the core language by supplying objects relevant to running JavaScript on a server. For example, server-side extensions allow an application to communicate with a relational database, provide continuity of information from one invocation to another of the application, or perform file manipulations on a server.

> Through JavaScript's LiveConnect functionality, you can let Java and JavaScript code communicate with each other. From JavaScript, you can instantiate Java objects and access their public methods and fields. From Java, you can access JavaScript objects, properties, and methods.

> Netscape invented JavaScript, and JavaScript was first used in Netscape browsers.

> JavaScript and Java

> JavaScript and Java are similar in some ways but fundamentally different in others. The JavaScript language resembles Java but does not have Java's static typing and strong type checking. JavaScript supports most Java expression syntax and basic control-flow constructs.

> In contrast to Java's compile-time system of classes built by declarations, JavaScript supports a runtime system based on a small number of data types representing numeric, Boolean, and string values. JavaScript has a prototype-based object model instead of the more common class-based object model. The prototype-based model provides dynamic inheritance; that is, what is inherited can vary for individual objects. JavaScript also supports functions without any special declarative requirements. Functions can be properties of objects, executing as loosely typed methods.

> JavaScript is a very free-form language compared to Java. You do not have to declare all variables, classes, and methods. You do not have to be concerned with whether methods are public, private, or protected, and you do not have to implement interfaces. Variables, parameters, and function return types are not explicitly typed.

> Java is a class-based programming language designed for fast execution and type safety. Type safety means, for instance, that you can't cast a Java integer into an object reference or access private memory by corrupting Java bytecodes. Java's class-based model means that programs consist exclusively of classes and their methods. Java's class inheritance and strong typing generally require tightly coupled object hierarchies. These requirements make Java programming more complex than JavaScript authoring.

> In contrast, JavaScript descends in spirit from a line of smaller, dynamically typed languages such as HyperTalk and dBASE. These scripting languages offer programming tools to a much wider audience because of their easier syntax, specialized built-in functionality, and minimal requirements for object creation.

> Table 1.1 JavaScript compared to Java

> JavaScript | Java
> --- | ---
> Object-oriented. No distinction between types of objects. Inheritance is through the prototype mechanism, and properties and methods can be added to any object dynamically. | Class-based. Objects are divided into classes and instances with all inheritance through the class hierarchy. Classes and instances cannot have properties or methods added dynamically.
> Variable data types not declared (dynamic typing). | Variable data types must be declared (static typing).
> Cannot automatically write to hard disk. | Cannot automatically write to hard disk.

> For more information on the differences between JavaScript and Java, see Chapter 8, "Details of the Object Model."

> JavaScript and the ECMA Specification

> Netscape invented JavaScript, and JavaScript was first used in Netscape browsers. However, Netscape is working with ECMA (European Computer Manufacturers Association) to deliver a standardized, international programming language based on core JavaScript. ECMA is an international standards association for information and communication systems. This standardized version of JavaScript, called ECMAScript, behaves the same way in all applications that support the standard. Companies can use the open standard language to develop their implementation of JavaScript. The first version of the ECMA standard is documented in the ECMA-262 specification.

> The ECMA-262 standard is also approved by the ISO (International Organization for Standards) as ISO-16262. You can find a PDF version of ECMA-262 at the mozilla Web site. You can also find the specification on the ECMA Web site. The ECMA specification does not describe the Document Object Model (DOM), which is standardized by the World Wide Web Consortium (W3C). The DOM defines the way in which HTML document objects are exposed to your script.

> Relationship Between JavaScript and ECMA Versions
> Netscape works closely with ECMA to produce the ECMA specification. The following table describes the relationship between JavaScript and ECMA versions.

> Table 1.2 JavaScript and ECMA versions

> JavaScript version | Relationship to ECMA version
> --- | ---
> JavaScript 1.1 | ECMA-262, Edition 1 is based on JavaScript 1.1.
> JavaScript 1.2 | ECMA-262 was not complete when JavaScript 1.2 was released. JavaScript 1.2 is not fully compatible with ECMA-262, Edition 1, for the following reasons: Netscape developed additional features in JavaScript 1.2 that were not considered for ECMA-262. ECMA-262 adds two new features: internationalization using Unicode, and uniform behavior across all platforms. Several features of JavaScript 1.2, such as the Date object, were platform-dependent and used platform-specific behavior.
> JavaScript 1.3 | JavaScript 1.3 is fully compatible with ECMA-262, Edition 1. JavaScript 1.3 resolved the inconsistencies that JavaScript 1.2 had with ECMA-262, while keeping all the additional features of JavaScript 1.2 except == and !=, which were changed to conform with ECMA-262.
> JavaScript 1.4 | JavaScript 1.4 is fully compatible with ECMA-262, Edition 1. The third version of the ECMA specification was not finalized when JavaScript 1.4 was released.
> JavaScript 1.5 | JavaScript 1.5 is fully compatible with ECMA-262, Edition 3.

> Note: ECMA-262, Edition 2 consisted of minor editorial changes and bug fixes to the Edition 1 specification. The TC39 working group of ECMA is currently working on ECMAScript Edition 4, which will correspond to a future release of JavaScript, JavaScript 2.0.

> The Core JavaScript Reference indicates which features of the language are ECMA-compliant.

> JavaScript will always include features that are not part of the ECMA specification; JavaScript is compatible with ECMA, while providing additional features.

> JavaScript Documentation vs. the ECMA Specification
> The ECMA specification is a set of requirements for implementing ECMAScript; it is useful if you want to determine whether a JavaScript feature is supported under ECMA. If you plan to write JavaScript code that uses only features supported by ECMA, then you may need to review the ECMA specification.

> The ECMA document is not intended to help script programmers; use the JavaScript documentation for information on writing scripts.

> JavaScript and ECMA Terminology
> The ECMA specification uses terminology and syntax that may be unfamiliar to a JavaScript programmer. Although the description of the language may differ in ECMA, the language itself remains the same. JavaScript supports all functionality outlined in the ECMA specification.

> The JavaScript documentation describes aspects of the language that are appropriate for a JavaScript programmer. For example:

> The global object is not discussed in the JavaScript documentation because you do not use it directly. The methods and properties of the global object, which you do use, are discussed in the JavaScript documentation but are called top-level functions and properties.

> - The no parameter (zero-argument) constructor with the Number and String objects is not discussed in the JavaScript documentation, because what is generated is of little use. A Number constructor without an argument returns +0, and a String constructor without an argument returns "" (an empty string).

> New Features in this Release

> JavaScript version 1.5 provides the following new features and enhancements:

> Runtime errors. Runtime errors are now reported as exceptions.

> - Number formatting enhancements. Number formatting has been enhanced to include Number.prototype.toExponential, Number.protoytpe.toFixed and Number.prototype.toPrecision methods. See page 109.

> - Regular expression enhancements. The following regular expression enhancements have been added:

>   Quantifiers — +, *, ? and {}— can now be followed by a ? to force them to be non-greedy. See the entry for ? on page 56.

>   - Non-capturing parentheses, (?:x) can be used instead of capturing parentheses(x). When non-capturing parentheses are used, matched subexpressions are not available as back-references. See the entry for (?:x) on page 56.

>   - Positive and negative lookahead assertions are supported. Both assert a match depending on what follows the string being matched. See the entries for x(?=y) and x(?!y) on page 56.

>   - The m flag has been added to specify that the regular expression should match over multiple lines. See page 63.

> - Conditional function declarations. Functions can now be declared inside an if clause. See page 84.

> - Function expressions. Functions can now be declared inside an expression. See page 85.

> - Multiple catch clauses. Multiple catch clauses in a try...catch statement are supported. See page 80.

> - Getters and Setters. JavaScript writers can now add getters and setters to their objects. This feature is available only in the C implementation of JavaScript. See page 98.

> - Constants. Read only named constants are supported. This feature is available only in the C implementation of JavaScript. See page 27.

> Previous Contents Index Next

> Copyright © 2000 Netscape Communications Corp. All rights reserved.

Last Updated September 28, 2000