OPERATORS:  ?:  ,  delete  new  this  typeof  void

This is a conditional operator that takes three operands and is used to replace simple if statements. The first operand is a condition that evaluates to true or false, the second is an expression of any type to be returned if the condition is true, and third is an expression of any type to be returned if the condition is false. The following code displays one of two messages depending on the value of the object property 'percent_proof':
   document.write("This beer is " + ((percent_proof < 5) ? "mild" : "strong"));

This is the comma operator which is most often used to include multiple expressions where only one is required, particularly in a for loop. It evaluates both its operands and returns the value of the second. The following code uses a two-dimensional array of 3 by 3 elements and initializes two counters (one for each dimension) incrementing them both, which results in a display of the left-to-right diagonal values:
for(var i=0, j=0; i<3; i++, j++)
   document.write(a[i][j] + "<BR>");

...while using the comma operator inside the square brackets of the next example causes the code to display all the elements of the array one row at a time:
for(var i=0, j=0; i<3; i++, j++)
   document.write(a[i, j] + "<BR>");

The delete operator is used to delete an object, an object's property or a specified element in an array, returning true if the operation is possible, and false if not. With the defined object 'fruit' below, the following delete operations are possible:
fruit = new Object;
fruit.name = 'apple';
fruit.color = 'green';
fruit.size = 'large';

delete fruit.size;
   delete color;
delete fruit;

To delete an object's property, you must precede that property's name with the name of the object, unless it's used in a with statement.
The delete operator can also be used to delete an element of an array. This does not affect the length of the array or any of the other elements but changes the deleted element to undefined. The following example creates an array called 'fruit' and then deletes element #2 (orange):
fruit = new Array ("apple", "pear", "orange", "cherry", "grape");
delete fruit[2];

The new operator can be used to create an instance of a user-defined object type or of one of the built-in object types that has a constructor function. To create a user-defined object type you must first define it by writing a function that specifies its name, properties and methods. For example, the following function creates an object for books with properties for title, category and author:
function book(title, category, author)
   this.title = title
   this.category = category
   this.author = author

You can than create an instance of the object as in the following example which assigns the values "The Thing", "horror" and "John Lynch" to the respective properties:
mybook = new book("The Thing", "horror", "John Lynch")
The property of an object can even be another object. You could, for example, create an object for authors and use it in the above example of the 'book' object. Again you would first define it by writing a function, and then you could create an instance of it for "John Lynch" as follows:
function author(name, real_name, age)
   this.name = name
   this.real_name = real_name
   this.age = age
author1 = new author("John Lynch", "Charlie Schwarz", 43)

Now, as long as the 'author' object and the relevent instance of it have already been defined, the 'author' property in the 'book' object will refer to it, and you can display, say, the age of the author of 'mybook' as follows:
You can also add a property to any instance of an object as in the next example which adds the property 'publisher' to the 'mybook' object and assigns it the value "Centurion Books":
mybook.publisher = "Centurion Books"
If, however, you wanted to add a property to a previously defined object type (and, therefore, all instances of it) you would have to use the prototype property. This next example adds the property 'publisher' to the 'book' object type, and then assigns the value 'Centurion Books' to one particular instance of it:
book.prototype.publisher = null
mybook.publisher = "Centurion Books"

The keyword this is used to refer to the current object. In a method, it usually refers to the calling object. In the following example the code first creates a function DescribeAge that takes as its parameter an object, and returns one of two text values depending on the value of that same object's Year property. Then another object called Car is created whose third property Description is the value returned by the DescribeAge function. The keyword this is used as the parameter of the DescribeAge function to refer to whichever object is calling it, as seen in the final bit of code which creates a specific instance of the Car object whose Description property will now contain the string "Old-fashioned":
function describeAge(obj)
   if(obj.year < 1996)
      return "Old-fashioned"
      return "Good-as-new"

function car(make, year, description)
{this.make = make, this.year = year, this.description = describeAge(this)}
myCar = new car("Ford", "1993", describeAge(this))

The typeof operator returns the type of an unevaluated operand which can be a number, string, variable, object or keyword. It can be used with or without brackets as in the following examples of a numeric literal and the variable 'age' being 60:
typeof(age)   returns    number
typeof 33
   returns    number
The following values are returned for various types of data:
a number returns 'number';
a string returns 'string';
the keyword true returns 'boolean';
the keyword null returns 'object';
methods, functions and predefined objects return 'function';
a variable returns the type of the data assigned to it;
a property returns the type of its value;
The void operator evaluates an expression without returning a value. Although the use of brackets after it is optional, it is good style to use them. The following example creates a hyperlink on the word "green" which, when clicked, changes the background color to light green:
Sam turned <a href="javascript:void(document.bgColor='lightgreen')">green</a>.

Copyright 1999-2001 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information