Just as syntax is important to learning the language, knowing how to put all the pieces of a command together is equally important.
Statements in JavaScript are the ways and means to get a task done. The constructor of the command block also must pull together all the pieces to make the code work. Some of these constructors in JavaScript are loops , conditional phrases, break points, and for...in and continue calls, to name a few. In this section, we examine how constructors work inside statements and what to use them for.
Comment Codes
Commenting your code in JavaScript is easy! Good developers use good sense and comment their code often, leaving instructions or notes to those who may come after them. Comments also help denote the code's structure or routines. If you've ever worked with someone else's code, it's not an easy task. Always comment your code!
To comment a single line, use double forward slashes ( // ):
//Your comment goes here!
To create a multiline comment, use a forward slash and an asterisk ( /* , */ ):
/*Your comment goes here and, all the way down here to the next line.*/
Conditional Statements
Only one conditional statement exists in JavaScript, and that is if...else . The if...else statement is performed based on the logic of the code, or whether the statement is true or false . If the if part of the statement is false , the else part of the statement is executed. Here's an example:
Function PurchaseTeeShirt() { If(ans=="Yes") { /*This assumes the var ans has previously been declared.*/ var qty = Prompt("How many would you like?","1"); var size = Prompt("What size XL,L,M,S?","XL"); } else { alert("Thank you for visiting the JavaScript Zone!"); } }
If the user answers "Yes" to the prompt "Would you like to purchase a Tee-Shirt?", the if statement is executed and prompts the user again, asking how many. If the user enters anything but "Yes," the code executes the else statement and thanks the user for visiting the zone.
Loop Statements
JavaScript has two loop statements: the while loop and the for loop. Both statements are found in most scripting languages and work similarly. Loops are used to perform repeated tasks and counters, instead of creating code over and over again to perform each task singularly.
while Loop
The while loop is executed while an expression's condition is true and repeats the process until the condition is false . For example:
Function iceCreamCone(ans) { var scoops=0; var j=1; if (ans == "Yes") { var conesize = Prompt("What size Ice Cream Cone would you like: L, M, S?","L") { if (conesize == "L") { while (j<3){ /*while j is less than 3, loop*/ j++; //increment j by one. scoops=j; } var totalscoops=scoops; }else{ ... }else{ ... }
In this example, the while loop executes while var j is less than 3 .
for Loop
The for loop is just as simple to use as the while loop. It can also be used as a counter. In this example, the for loop is used in place of the while loop:
Function iceCreamCone(ans) { var scoops=0; if (ans == "Yes") { var conesize = Prompt("What size Ice Cream Cone would you like: L, M, S?","L") { if (conesize == "L") { for(j = 1; j < 3;j++) { } var scoops=j; }else{ ... }else{ ... }
This loop says to use var j as a counter. Start with j equal to 1 , perform the command block, and then increment j as long as j is less than or equal to 3 .
for...in Statements
The for...in statement is used to step through all the properties of an object. It also allows you to step through the properties without testing for a condition or incrementing a counter. For example:
for (wines in list) { add code here to get the type of wine }
Each property in an object can also be referred to as a number within its index. For example:
for (j in winelist) { Add code here to get the French wines }
The winelist object's index might look like this:
California.Chardonnay[0] French.Chardonnay[1] French.Merlot[2] California.Cabernet[3] California.Zinfindel[4]
Here, j would represent (0) to the end of the index, which in this case is (4) , as it steps through the object's properties. Object indexes always begin at [0] . This is the same as in LotusScript.
break Statements
During a loop, there might be cause to break out of its evaluation. The break statement breaks out of a loop and terminates a for or while loop, and executes the statement following the loop. For example:
function luckyseven(roll) { for (var j=0; j<=roll; j++) { if (j==7) { break; } alert("Seven is your lucky number!"); }
In this example, when var j equals 7 , the for loop is terminated and the alert command is evaluated.
continue Statements
The continue statement is used to jump to the next repetition in a command block without completing the current evaluation inside the command block. For example in a while loop, continue goes back to the condition; in a for loop, continue jumps out of the evaluation and continues on in the expression:
function addnum(num) { for(var j=0; j<=num;j++){ If (j==3) { continue; } alert("You've reached 3 and continue brings you here!"); }
this Statement
The this statement refers to the current object. For example:
function contact(name) { this.name = name; }
Using this in the previous function refers to the contact object. You could also use this:
contact.name
However, using this is much easier to refer to the current object. In fact, in Domino, many times you'll see a global variable named myform set to myform=document.forms['myform'] . This is another approach and shortcut to myform.myfield.value or this.myfield.value .
new Statement
The new statement creates a new instance of an object. For example, to create a new instance of a contact object defined by the contact function, the command would be this:
//The contact function defined. function contact(name) { this.name = name; } //Creating a new instance of the contact object. contact1 = new contact("Sherm Joe-Bob");
Part I. Introduction to Release 6
Whats New in Release 6?
The Release 6 Object Store
The Integrated Development Environment
Part II. Foundations of Application Design
Forms Design
Advanced Form Design
Designing Views
Using Shared Resources in Domino Applications
Using the Page Designer
Creating Outlines
Adding Framesets to Domino Applications
Automating Your Application with Agents
Part III. Programming Domino Applications
Using the Formula Language
Real-World Examples Using the Formula Language
Writing LotusScript for Domino Applications
Real-World LotusScript Examples
Writing JavaScript for Domino Applications
Real-World JavaScript Examples
Writing Java for Domino Applications
Real-World Java Examples
Enhancing Domino Applications for the Web
Part IV. Advanced Design Topics
Accessing Data with XML
Accessing Data with DECS and DCRs
Security and Domino Applications
Creating Workflow Applications
Analyzing Domino Applications
Part V. Appendices
Appendix A. HTML Reference
Appendix B. Domino URL Reference