HTML and CSS Reference
In-Depth Information
EXPLANATION
1
Two strings are created, one the literal way (a string primitive) and the other with
the constructor method (a String object).
2
The length property is applied to the first string. When the property is applied to
a literal string, it is temporarily converted to an object, and then after the opera-
tion, it is reverted back to a string primitive.
3
The length property is applied to the second string, a String object. (It is just a co-
incidence that both strings are of the same length.) (See Figure 9.28.)
Figure 9.28 Using the String object's length property.
Extending the String Object. B au e l ob s h e the prototype object, it is
possible to extend the properties of a JavaScript built-in String object, just as we did for
a user-defined object (see Chapter 8).
EXAMPLE 9.25
<html>
<head><title>The Prototype Property</title>
<script type = "text/javascript">
// Customize String Functions
1
function ucLarge() {
var str=this.bold().fontcolor("white").
toUpperCase().fontsize("22");
return(str);
}
2
String.prototype.ucLarge=ucLarge;
</script>
</head>
<body bgcolor="black">
<div align="center">
<script type="text/javascript">
3
var str="Watch Your Step!!";
4
document.write( str.ucLarge() +"<br />");
</script>
<img src="high_voltage.gif">
</div>
</body>
</html>
 
Search WWH ::




Custom Search