Microsoft® JScript Functions |
| Tutorial |
|
Microsoft JScript supports two kinds of functions: those that are built into the language, and those you create yourself.
A function that is part of an object is called a method. There are many methods built into JScript; some of these are properties of the Math, Date, and string objects, and are discussed in detail in the language reference.
function isItTheMillenniumYet() { // The function definition begins here. var nowadays = new Date(); nowadays = nowadays.getYear() + 1900; if (nowadays >= 2001) { // Calculation, based on the "end of 2000" definition of millennium. return nowadays+"True"; } else return nowadays+"False"; } // End of the function definition. var newCycle = isItTheMillenniumYet(); // The function is called here. var theYear = newCycle.substring(0,4); // Capture the year from the beginning of the return. newCycle = newCycle.substring(4,newCycle.length); // Capture "True" or "False". var comment1 = "Nowadays, it's " + theYear; // Construct the first part of the response. var query1 = "The Millennium Has Arrived. True or false?"; // Construct the second part. var comment2 = newCycle + "... and there you have it."; // Construct the final part.
escape(aString) unescape(aString) eval(aString) parseFloat(aString) parseInt(aString,anOptionalRadix)The first two of these, escape() and unescape(), are used to convert characters that have special meanings in HTML code, characters that you cannot just put directly into text. For example, the angle brackets, "<" and ">", delineate HTML tags.
The escape function takes as its argument any of these special characters, and returns the escape code for the character. Each escape code consists of a percent sign (%) followed by a two-digit number. The unescape function is the exact inverse. It takes as its argument a string consisting of a percent sign and a two-digit number, and returns a character. Please consult the language reference for more information about these and other built-in functions.
One extremely useful built-in function is eval(), which evaluates any valid mathematical expression that is presented in string form. The eval() function takes one argument, the expression to be evaluated.
var anExpression = "6 * 9 % 7"; var total = eval(anExpression); // Assigns the value 5 to the variable total. var yetAnotherExpression = "6 * (9 % 7)"; total = eval(yetAnotherExpression) // Assigns the value 12 to the variable total. var totality = eval("...surrounded by acres of clams."); // Generates an error.
The checkTriplet function in the following example takes as its arguments the lengths of the sides of a triangle, and calculates from them whether the triangle is a right triangle by checking whether the 3 numbers constitute a Pythagorean triplet. (The square of the length of the hypotenuse of a right triangle is equal to the sum of the squares of the lengths of the other two sides.) The checkTriplet function calls one of two other functions to make the actual test.
Notice the use of a very small number ("epsilon") as a testing variable in the floating-point version of the test. Because of uncertainties and roundoff errors in floating-point calculations, it is not practical to make a direct test of whether the square of the hypotenuse is equal to the sum of the squares of the other two sides unless all three values in question are known to be integers. Because a direct test is more accurate, the code in this example determines whether it is appropriate and, if it is, uses it.
var epsilon = 0.0000000000001; // Some very small number to test against. var triplet = false; function integerCheck(a, b, c) { // The test function for integers. if ( (a*a) == ((b*b) + (c*c)) ) { // The test itself. triplet = true; } } // End of the integer checking function. function floatCheck(a, b, c) { // The test function for floating-point numbers. var theCheck = ((a*a) - ((b*b) + (c*c))) // Make the test number. if (theCheck < 0) { // The test requires the absolute value, so invert theCheck if it's negative. theCheck *= -1; } if (epsilon > theCheck) { // If it's as close as that, it's pretty darn close! triplet = true; } } // End of the floating-poing check function. function checkTriplet(a, b, c) { // The triplet checker. First, move the longest side to position "a". var d = 0; // Create a temporary holding bin. if (c > b) { // If c > b, swap them. d = c; c = b; b = d; } // If not, ignore them. if (b > a) { // If b > a, swap them. d = b; b = a; a = d; } // If not, ignore them. // Side "a" is now the hypotenuse, if there is one. if (((a%1) == 0) && ((b%1) == 0) && ((c%1) == 0)) { // Test all 3 values. Are they integers? integerCheck(a, b, c); // If so, use the precise check. } else floatCheck(a, b, c); // If not, get as close as is reasonably possible. } // End of the triplet check function. // The next three statements assign sample values for testing purposes. var sideA = 5; var sideB = 5; var sideC = Math.sqrt(50); checkTriplet(sideA, sideB, sideC); // Call the function. After the call, triplet contains the result.
© 1996 by Microsoft Corporation.
file: /Techref/language/asp/js/307.htm, 7KB, , updated: 1996/11/22 10:12, local time: 2024/11/26 16:29,
18.119.163.95:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://massmind.ecomorder.com/techref/language/asp/js/307.htm"> Microsoft® JScript Language Tutorial </A> |
Did you find what you needed? |
Welcome to ecomorder.com! |
Welcome to massmind.ecomorder.com! |
.