HTML and CSS Reference
In-Depth Information
Figure 9.39 Output from Example 9.34.
9.5.8 The with Keyword Revisited
In Chapter 8, we used the with keyword with user-defined objects to make it easier to
manipulate the object properties. Recall that any time you reference the object within
the block following the keyword, you can use properties and methods without the object
name. This saves a lot of typing and reduces the chances of spelling errors, especially
when the properties have long names. The String object is used in the following example
to demonstrate how with is used (see Figure 9.40).
EXAMPLE 9.35
<html>
<head><title>The with Keyword</title></head>
<body>
<h2>Using the <em>with</em> keyword</h2>
<p>
<h3>
<script type="text/javascript">
var yourname =prompt("What is your name? ","");
// Create a string object
1
with(yourname){
document.write("Welcome " + yourname +
" to our planet!!<br />");
2
document.write("Your name is " + length +
" characters in length.<br />");
3
document.write("Your name in uppercase: " + toUpperCase()
+ ".<br />");
4
document.write("Your name in lowercase: " + toLowerCase()
+ ".<br />");
}
</script>
</h3>
</body>
</html>
EXPLANATION
1
The with keyword allows the methods of an object (in this example a string ob-
ject) to be called without the object's name and a dot.
 
 
Search WWH ::




Custom Search