Free Book Excerpt: Lotus Notes and Domino 6 Development (2)

Using Multiple Parameters

Functions can pass multiple arguments as well. The only stipulation is that you must call or use them in the same order as they are defined in the function. For example:

Function winelist(selection, year) {
If (selection == "Chardonnay" && year=="1985"){
     return true;
}Else{
     return false;
}
}

You can call a multiparameter function in several ways. Here are some examples:

//declaring a variable
var wines = winelist("Red", "1996");
//Using the if... statement
if ( winelist("Red", "1996") ){
   ... other code here
}
//Using the onLoad event
onLoad = "winelist(selection, year)";

When more than one function parameter is being used, be careful of the order in which the parameters are defined. When one parameter is a string and the other is a number, and they are called in the reverse order, the result is an error when the code is compiled. This example shows the incorrect way to call a multiparameter function:

var selection = prompt("What type of wine would you like with dinner?", "Chardonnay");
var numbottles = 1;
winelist(numbottles, selection);

function winelist(selection, numbottles){
   If (selection == "Chardonnay"){
     return true;
   }else{
     Prompt("Sorry we only have Chardonnay in stock!");
     return false;
   }
}

This calls the function incorrectly:

winelist(numbottles,selection) 

This results in an error at runtime.

Returning the Results

To return a value of a function's result, a return statement must be used in the code block:

function winelist(selection) {
   If (selection == "Chardonnay"){
     return true;
   }else{
     return false;
   }
}

In this case, if the wine list selection equals "Chardonnay", true is returned by the function.

Built-In Functions

Sometimes using the built-in functions make more sense than creating your own functions. Why reinvent the wheel? Eval() is a good example of a JavaScript built-in function that you should not have to re-create.

Eval()

Eval() evaluates a string to a numeric value. This expression might be any string, variable, property of an existing object, or statement. Here's an example:

var gBeans = 0;
function countJellyBeans(jar1, jar2, jar3){
   gBeans = eval("jar1+jar2+jar3");
}

In this example, eval() is used to sum the total of jellybeans in each jar.

ParseInt()

ParseInt() evaluates a string argument and returns an integer. The integer returned depends on its base (decimal, octal, or hexadecimal). Here's an example:

var jar1 = "326.25";
gBeans = 0;
Function countJellyBeans(jar1){
   gBeans = parseInt(jar1);
}

In this example, the integer 326 would be returned as the total number of jellybeans in jar1.

ParseFloat()

ParseFloat() evaluates a string argument and returns a floating-point number. Here's an example:

var jar1 = "+326.25987098+2";
gBeans = 0;
Function countJellyBeans(jar1){
   gBeans = parseInt(jar1);
}

In this example, the number 326.25987098 would be returned as the total number of jellybeans in jar1.

Objects

As described earlier in the chapter, custom objects can easily be created in JavaScript for use in functions. To do this, you must first define the object's properties. Once these are defined, new instances of the object can be created and new methods can be added.

Defining an Object

To define a new object, you must first outline its properties. Let's define an object named contact and set a few of its properties, as in name, telephone, and title:

function contact(name, telephone, title) {
   this.name = name;
   this.telephone = telephone;
   this.title = title;
}

This results in a new contact object that is defined along with its properties. The contact object can now be created using the new statement.

contact1 = new contact("Mabel Chin","(781) 333-5252","Estate Attorney");

This creates an object called contact1 with three bound properties: contact1.name, contact1.telephone, and contact1.title. This method is called the instance of the object. Let's create another contact using the new statement once again:

contact2 = new contact("Silke Hase","(781)384-5470)","Development Manager");

When creating contact2, which is actually just another new instance of the contact object, contact2's properties are very independent of those in contact1. Here's another way to set the properties of an object:

Contact2.birthday = "April 5"

This adds the birthday property to contact2 without affecting the properties of contact1 or having it defined as an instance to an object. To add the birthday property to all instances of the object, you would have to add the property to the object's definition, as in this example:

function contact(name, telephone, title, birthday) {
   this.name = name;
   this.telephone = telephone;
   this.title = title;
   this.birthday = birthday; 
}

Objects as Properties of Objects

It is also possible to create objects as properties to other objects. This means associating an object's property with another object. For example, let's create a company object:

function company(name, city, state){
   this.name = name;
   this.city = city;
   this.state = state;
}

Now let's create two new instances of the company object:

company1 = new company("Fancy Bank","Bedford","MA");
company2 = new company("Emerging Innovations","Stoneham","MA");

Using these two new company objects, we can easily create the following new contact objects adding the company's properties to each:

contact1 = new contact("Mabel Chin","(781) 333-5252","Estate Attorney","September 17",company1.coname,company1.city,company1.state);
contact2 = new contact("Silke Hase","(781) 384-5470","Development Manager","May 5",company2.coname,company2.city,company2.state);

By making the company properties part of the contact object, it is now easy to refer to the company properties for each contact object:

contact2.coname

Adding Methods to Objects

Methods can be added to an object's definition by creating functions that define the method. Methods are merely functions associated with an object. Let's add a method called displaycontact to the contact object that will print the contact's name, phone, title, birthday, company, city, and state to a document window:

Function displaycontact(contact) {
document.write("Contact Name:t" + this.name + "<BR>");
document.write("Telephone:t" + this.telephone + "<BR>");
document.write("Title:tt" + this.title + "<BR>");
document.write("Birthday:t" + this.birthday + "<BR>");
document.write("Company:t" + this.coname + "BR>");
document.write("City:tt" + this.city + "<BR>");
document.write("State:tt" + this.state + "<BR>");
}

To use this method, it must be included in the contact object's definition:

function contact(name,telephone,title,birthday,coname,city,state){
this.name = name;
this.telephone = telephone;
this.title = title;
this.birthday = birthday;
this.coname = coname;
this.city = city;
this.state = state;
this.displaycontact = displaycontact;
}

The following command would be used to output contact2's information to the document window:

contact2.displaycontact();

Figure 16.17 shows what the output looks like in a document window.

Figure 16.17
The contact2 object's properties output to the browser.


Chapter 17: Real-World JavaScript Examples

Utility 5: validateDate()

Utility 5, validate(date(), validates the content of a date field and checks to make sure that what is entered is a date using a separator such as a period. Table 17.7 explains this utility's purpose, how to call it, its parameters and its code.

Table 17.7 About the validateDate() Function
Purpose:
Validates the contents of a date field to make sure that what was entered is actually a date. If what is entered is not a valid date, an error message is presented to the user.
How to call it:
validateDate(document.forms[0], 'myDateField', 'My Date Field Label')
Parameters:
form is the pointer to the form object
dateFieldName is the name of the date field that needs to be validated.
fieldLabel is the name of the date field's label.
Code:

function validateDate(form, dateFieldName,fieldLabel){
var checkstr = "0123456789";
var DateField = fieldName;
var Datevalue = "";
var DateTemp = "";
//Change the separator to reflect what your date field uses to separate the 
date's month, year, and day.
var separator = ".";
var day;
var month;
var year;
var leap = 0;
var err = 0;
var i;
err = 0;
DateValue = DateField.value;
     for (i = 0; i < DateValue.length; i++) {
          if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
                   DateTemp = DateTemp + DateValue.substr(i,1);
            }
   }
     DateValue = DateTemp;
     if (DateValue.length == 6) {
            DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
     if (DateValue.length != 8) {
            err = 19;}
            year = DateValue.substr(4,4);
     if (year == 0) {
            err = 20;
     }
   /* Validation of month*/
   month = DateValue.substr(2,2);
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   day = DateValue.substr(0,2);
   if (day < 1) {
     err = 22; 
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") 
|| (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") 
|| (month == "09") || (month == "11"))) {
      err = 26;
   }
   /* if 00 is entered, no error, delete the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; separator = "";
   }
   /* if no error, write the date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
    DateField.value = day + separator + month + separator + year;
   }
   else {
      alert("The date entered is incorrect!");
      DateField.select();
      DateField.focus();
   }
} 



Page 1 | Page 3

-ADS BY GOOGLE

SearchWindowsServer

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchDataCenter

SearchContentManagement

Close