Last Modified: 11/9/98
[Previous] [Home] [Next]

JavaScript as a Programming Language


Credit: Kirk Born (St Sci/NASA)

Elements of JavaScript

We will explain JavaScript mostly through examples. We assume that you are familar with "C-like" languages and so will not explain those elements of the language that are similar to "C". Client-Side JavaScript:
  • Operates on the client-side so it reacts much faster, with more client-side capabilites, and greater security, than other languages
  • Is an interpretive langauge, so its basic operation is slower than a compiled language, but is much more flexible and has a more free and easy syntax
  • Has a large number of reserved words: the following words are absolutely, positively reserved:
      
    break    false    in     this   void
    continue for      new    true   while
    delete   function null   typeof with
    else     if       return var
         
    plus about 120 or so words that are sorta reserved or might be reserved in the future. See the complete list in David Flannagan's "JavaScript, the Definitive Guide".

The main components of JavaScript are:

  • Expressions and operators: the usual "C-like" with a few exceptions: "new ", "typeof", and "+" used for string concatenation
  • Statements: the usual "C-like" statements with a few exceptions: "for (prop in object)...", "with (object)..."
  • Strings: declaration, concatenation, length, split, concatenation with numbers, charAt, substring, indexOf. See part [3] Test of Strings
  • Functions: these differ in many ways from "C-like" languages. See part [5] Functions and Arguments. for many of the important features of JavaScript dfunctions.
  • Classes and objects: these are a major aspect of object-oriented languages, such as JavaScript. See part [4] Test of Objects for many of their features.
  • Arrays: these differ from most other languages. See part [2] Test of Arrays for many of their features.

JavaScript, like many languages, makes a distinction between value versus reference, particulary in terms of copies, comparisons, and arguments. Basically, Booleans and numbers are copied, passed, and compared by value, where as all others (object, array, string, function) are copied, passed, and compared by reference (except strings, which are compared by value)

Object Oriented (OO) Programming Concepts

JavaScript is very much an OO type language. The main goals of OO programming are to
  • Identify classes that group similar entities together
  • Create objects from the classes, using the
    new Class()
    syntax
  • Populate the classes and objects with variables and functions (called "methods") which describe their behavior
You determine the classes you use based on your purposes. For example, suppose you are developing a web-based system to keep track of people in your department, and suppose, for the purposes of the program, that students, staff, and faculty must be treated in very different ways. Then it makes sense to setup your classes to reflect this natual division, as follows:
// Define the classes----------------
function Student(year,major,..) {
   ...
}
function Staff(classification, ...) {
   ...
}
function Faculty(tenure, research,...) {
   ...
}
 ...
// Create the objects for these classes -
for (i = 0; i < n_Students; i++) {
 student[i]=new Student(2,"premajor",...)
}
for (i = 0; i < n_Staff; i++ ) {
  staff[i]=new Staff("temporary",...) {
}
for (i = 0; i < n_Faculty; i++) {
  faculty[i]=new Faculty("tenured",...) {
}
The parameters you pass to the functions that define the class supply some of the values that define the object. In addition, you may add functions and other variables both to the classes and to individual objects to further describe their behavior.

On the other hand, you may decide that, for you specific purposes, staff and faculty should be treated alike, whereas students are to be treated differently. In that case, your division might be FacStaff versus Student.


[Previous] [Home] [Next]