|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
Because an objects methods are properties, you can easily add new properties to an object by writing your own function and creating a new object property using your own function name. If you want to add a Bill method to your customer object, you can write a function named BillCustomer and set the objects property as follows: customer.Bill = BillCustomer; To call the new method, you write the following: customer.Bill() VBScript Language ElementsAlthough VBScript is not as flexible as C++ or Visual Basic, it is quick and simple. Because it is easily embedded in your Web pages, adding interactivity or increased functionality with a VBScript is easya lot easier than writing a Java applet to do the same thing. (Although, to be fair, you can do a lot more with Java applets.) This section covers some of the nuts and bolts of VBScript programming.
VBScript VariablesAll VBScript variables are of the type variant, which means that they can be used for any of the supported data types. Table 22.1 summarizes the types of data that VBScript variables can hold:
Forming Expressions in VBScriptAn expression is anything that can be evaluated to get a single value. Expressions can contain string or numeric variables, operators, and other expressions, and they can range from simple to quite complex. The following expression uses the assignment operator (more on operators in the next section), for example, to assign the result 3.14159 to the variable pi: pi=3.14159 By contrast, the following is a more complex expression whose final value depends on the values of the two Boolean variables Quit and Complete: (Quit = TRUE) And (Complete = FALSE) Using VBScript OperatorsOperators do just what their name suggests: They operate on variables or literals. The items that an operator acts on are called its operands. Operators come in the following two types:
Assignment Operators Assignment operators take the result of an expression and assign it to a variable. One feature that VBScript has that most other programming languages do not is the capability to change a variables type on-the-fly. Consider the example shown in Listing 33.1. Listing 33.1 Pi-fly.htmVBScript Variables Can Change Type On-the-Fly <HTML> <HEAD> <SCRIPT LANGUAGE=VBS> <!-- Hide this script from incompatible Web browsers! Sub TypeDemo Dim pi document.write(<HR>) pi = 3.14159 document.write(pi is & CStr(pi) & <BR>) pi = FALSE document.write(pi is & CStr(pi) & <BR>) document.write(<HR>) End Sub <!-- --> </SCRIPT> <TITLE>Changing Pi on-the-Fly!</TITLE> </HEAD> <BODY BGCOLOR=#FFFFFF> If your Web browser doesnt support VBScript, this is all you will see! <SCRIPT LANGUAGE=VBS> <!-- Hide this script from incompatible Web browsers! TypeDemo <!-- --> </SCRIPT> </BODY> </HTML>
This short function first prints the (correct) value of pi. In most other languages, however, trying to set a floating point variable to a Boolean value either generates a compiler error or a runtime error. Because VBScript variables can be any type, it happily accepts the change and prints pis new value: false (see Figure 33.1). The assignment operator, =, assigns the value of an expressions right side to its left side. In the preceding example, the variable pi gets the floating point value 3.14159 or the Boolean value false after the expression is evaluated. Math Operators The previous sections gave you a sneak preview of the math operators that VBScript furnishes. As you might expect, the standard four math functions (addition, subtraction, multiplication, and division) work the same as they do on an ordinary calculator and use the symbols +, , ×, and /.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |