Internet Systems
98
Chapter 12. JavaScript: Objects
We’ll use JavaScript to manipulate all kinds of objects.
This chapter is a more principled treatment of objects.
This chapter looks a several built-in objects.
The chapters on Dynamic HTML introduce many objects
provided by the browser that allow scripts to interact with
elements of an HTML document.
Objects we’ve already seen:
document
window
Array (constructor)
Math Object
Some methods of object Math – See the text (Fig. 12.1, p. 406) for
the common methods.
Let x and y evaluate to numeric values.
Math.max( x, y )
Math.min( x, y )
Math.sqrt( x )
Some constants – See the text (Fig. 12.2, pp. 406-407) for the
common constants.
Math.E is e, the base of the natural logarithms.
Math.LN10 is loge 10, i.e., ln 10.
Math.PI is π.
Internet Systems
99
Example:
<script type = "text/javascript">
var x = 3, y = 5;
document.writeln( "<p>Math.E = " +
Math.E + "</p>" );
document.writeln( "<p>Math.LN10 = " +
Math.LN10 + "</p>" );
document.writeln( "<p>Math.PI = " +
Math.PI + "</p>" );
document.writeln( "<p>x = " + x + ", y = "
+ y + "</p>" );
document.writeln( "<p>Math.max( x, y ) = " +
Math.max( x, y ) + "</p>" );
document.writeln( "<p>Math.pow( x, y ) = " +
Math.pow( x, y ) + "</p>" );
document.writeln( "<p>Math.sqrt( x ) = " +
Math.sqrt( x ) + "</p>" );
</script>
Math.E = 2.718281828459045
Math.LN10 = 2.302585092994046
Math.PI = 3.141592653589793
x = 3, y = 5
Math.max( x, y ) = 5
Math.pow( x, y ) = 243
Math.sqrt( x ) = 1.7320508075688772
Internet Systems
100
String Objects
A string is a sequence of characters, a unit of type String.
String literals or string constants (or anonymous String objects)
are sequences of characters in double or single quotes – e.g.,
“Fred Smith”
‘(336) 334-7245’
A string may be assigned to a variable