ÄúµÄλÖãºÑ°ÃÎÍøÊ×Ò³£¾±à³ÌÀÖÔ°£¾JavaScript£¾JavaScript Notes
Ongoing JavaScript Development

Development of the JavaScript language and its documentation continues. Additional features are planned; some current features could be modified if necessary.


Introduction

The JavaScript Language

JavaScript is a compact, object-based scripting language for developing client and server Internet applications. Netscape Navigator 2.0 interprets JavaScript statements embedded directly in an HTML page, and LiveWire enables you to create server-based applications similar to common gateway interface (CGI) programs.

In a client application for Navigator, JavaScript statements embedded in an HTML page can recognize and respond to user events such as mouse clicks, form input, and page navigation.

For example, you can write a JavaScript function to verify that users enter valid information into a form requesting a telephone number or zip code. Without any network transmission, an HTML page with embedded JavaScript can interpret the entered text and alert the user with a message dialog if the input is invalid. Or you can use JavaScript to perform an action (such as play an audio file, execute an applet, or communicate with a plug-in) in response to the user opening or exiting a page.


JavaScript and Java

The JavaScript language resembles Java, but without Java's static typing and strong type checking. JavaScript supports most of Java's expression syntax and basic control flow constructs. In contrast to Java's compile-time system of classes built by declarations, JavaScript supports a run-time system based on a small number of data types representing numeric, Boolean, and string values. JavaScript has a simple instance-based object model that still provides significant capabilities.

JavaScript also supports functions, again without any special declarative requirements. Functions can be properties of objects, executing as loosely typed methods.

JavaScript complements Java by exposing useful properties of Java applets to script authors. JavaScript statements can get and set exposed properties to query the state or alter the performance of an applet or plug-in.

Java is an extension language designed, in particular, for fast execution and type safety. Type safety is reflected by being unable to cast a Java int into an object reference or to get at private memory by corrupting Java bytecodes.

Java programs consist exclusively of classes and their methods. Java's requirements for declaring classes, writing methods, and ensuring type safety make programming more complex than JavaScript authoring. Java's inheritance and strong typing also tend to require tightly coupled object hierarchies.

In contrast, JavaScript descends in spirit from a line of smaller, dynamically typed languages like 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.

The following table compares and contrasts JavaScript and Java.

JavaScript Java
Interpreted (not compiled) by client. Compiled on server before execution on client.
Object-based. Code uses built-in, extensible objects, but no classes or inheritance. Object-oriented. Applets consist of object classes with inheritance.
Code integrated with, and embedded in, HTML. Applets distinct from HTML (accessed from HTML pages).
Variable data types not declared (loose typing). Variable data types must be declared (strong typing).
Dynamic binding. Object references checked at run-time. Static binding. Object references must exist at compile-time.
Secure. Cannot write to hard disk. Secure. Cannot write to hard disk.

JavaScript Development

A script author is not required to extend, instantiate, or know about classes. Instead, the author acquires finished components exposing high-level properties such as "visible" and "color", then gets and sets the properties to cause desired effects.

As an example, suppose you want to design an HTML page that contains some catalog text, a picture of a shirt available in several colors, a form for ordering the shirt, and a color selector tool that's visually integrated with the form. You could write a Java applet that draws the whole page, but you'd face complicated source encoding and forgo the simplicity of HTML page authoring.

A better route would use Java's strengths by implementing only the shirt viewer and color picker as applets, and using HTML for the framework and order form. A script that runs when a color is picked could set the shirt applet's color property to the picked color. With the availability of general-purpose components like a color picker or image viewer, a page author would not be required to learn or write Java. Components used by the script would be reusable by other scripts on pages throughout the catalog.