JavaScript

reference: Javascript 1.5 guide


Javascript is Netscape's scripting language for cross-platform development. It is not meant as a standalone language. Instead it is meant to be embedded in other applications, particularly web browsers. It is also meant to be a simpler programming language than Java or C. In terms of complexity it is more akin to Basic. Like the other scripting languages we have studied thus far, it does not require a user to declare variables. Instead it uses dynamic typing in which the type of a variable depends on the type of the value which it currently stores.

There are three versions of Javascript: 1) a core version, 2) a core version extended with features to support client-side programming, and 3) a core version extended with features to support server-side programming. All three versions contain the core version. The client-side version provides support for type checking of inputs from forms. This version executes on the client's machine so that input errors can be quickly found, rather than having to export the form's data to a server, have the server check the data, and then send back error messages to the client. The supply-side version provides support for accessing databases, maintaining information between two invocations of the same application, and performing other types of file operations on the server.

One drawback of Javascript is that the language was enhanced with each new release of Netscape. Hence a programmer sometimes cannot use newer features of Javascript if the programmer wants a script to be executable on old versions of Netscape. If portability is a concern, then you should check the documentation for a feature before using it. The documentation should tell you in which version of Javascript the feature was first provided.

Here is an example Javascript program that allows you to select a number of musical types and then, when you push a button, indicates how many types you have selected:

Choose some music types, then click the button below:

Here is the source for the script:

<SCRIPT>
function howMany(selectObject) {
   var numberSelected=0;
   for (var i=0; i < selectObject.options.length; i++) {
      if (selectObject.options[i].selected==true)
         numberSelected++;
   }
   return numberSelected;
}

</SCRIPT>
<FORM NAME="selectForm">
<P><B>Choose some music types, then click the button below:</B>
<BR><SELECT NAME="musicTypes" MULTIPLE>
<OPTION SELECTED> R&B
<OPTION> Jazz
<OPTION> Blues
<OPTION> New Age
<OPTION> Classical
<OPTION> Opera
</SELECT>
<P><INPUT TYPE="button" VALUE="How many are selected?"
onClick="alert ('Number of options selected: ' + howMany(document.selectForm.musicTypes))">
</FORM>

Javascript has a number of novel features that differentiate it from the scripting languages that we have described so far. One is its use of a prototype-instance model rather than a class-instance model and another is its provision of a reflection facility that allows an application to iterate through the properties of an object. Since html bundles up its forms data into objects, a programmer can use this reflection feature to iterate through the properties of a form object.