Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Using JavaScript's Built-in Objects

In this column, we'll tell you what <br>they are and how to use them

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 7

var tempVar = "this text is now upper case".toUpperCase();


or

var myString = "this text is now upper case";
var tempVar = myString.toUpperCase();


In Netscape 2.0 there is only one String object, and all strings are created from it. Conversely, strings are first-class objects in Atlas, and each new string is a treated as a separate object. The single-object behavior of strings in Netscape 2.0 can cause some subtle side effects. Take the short script segment that follows. Two strings are created: string1 and string2. A new property (called extra) is assigned to string1. Yet the alert message shows that the property also now belongs to string2.

<SCRIPT>
string1 = "this is string 1"
string2 = "this is string 2"
string1.extra = "new property"
alert (string2.extra)
</SCRIPT>

Technically speaking, strings are "immutable" in JavaScript. That is, the content of the string is static, and cannot be changed. In Netscape 2.0, JavaScript is capable of modifying a string only by creating a new location in memory for it. Because of this, a script that modifies a string many times is prone to memory errors. Each time the string is altered, JavaScript creates a new location in memory for the new version. New strings are created before garbage collection takes place to destroy the old string. Eventually, JavaScript uses all of its available memory, and an "out of memory" error occurs.

A classic example of this problem can be seen in the popular JavaScript "message scrollers," where a message scrolls in the status bar or a text box. For each pass, the scroller redefines the string variable that is displayed. Memory is eventually depleted because JavaScript creates new instances of the string with each pass. For example, the following script will eventually (sooner on some platforms, such as Windows 3.1) cause an "out of memory" error:

<SCRIPT>
var count = 0;
var text = "This is a test of a JavaScript scroller. ";
scroll();
function  scroll () {
        var myString = text.substring (count, text.length) + text.substring (0, count)
        window.status = myString
        if (count < text.length)
                count ++;
        else
                count = 0;
        setTimeout ("scroll()", 333);
        // 333ms is the minimum delay for Netscape 2.0
}
</SCRIPT>


A simple rewrite avoids the problem of creating new blocks of memory. Delete the myString variable assignment, and parse the text directly to the status bar, using window.status. See the revised JavaScript scroller to view the script in action. (Watch the status bar to see the text scroll by.)

window.status = text.substring (count, text.length) + text.substring (0, Count)


(While the above approach avoids JavaScript's string-object replication problem, memory leaks still occur because of the use of the setTimeout method. Over many iterations -- typically several thousand or more -- setTimeout will consume all available memory, and eventually JavaScript will display an "out of memory" message.)

For your reference, here are the methods and properties used with JavaScript's string object:

String Properties
length The length of a string
String Methods
anchor Creates a named anchor (hypertext target)
big Sets text to big
blink Sets text to blinking
bold Sets text to bold
charAt Returns the character at a specified position
fixed Sets text in fixed-pitch font
fontcolor Sets the font color
fontsize Sets font size
indexOf Returns the first occurrence of character x starting from position y
italics Sets text to italics
lastIndexOf Returns the last occurrence of character x starting from position y
link Creates a hyperlink
small Sets text to small
strike Sets text to strikeout
sub Sets text to subscript
substring Returns a portion of a string
sup Sets text to superscript
toLowerString Converts a string to lowercase
toUpperString Converts a string to uppercase


Using JavaScript as a scientific calculator

JavaScript's Math object provides advanced arithmetic and trigonometric functions, expanding on JavaScript's basic arithmetic operators (plus, minus, multiply, divide). The Math object in JavaScript is borrowed from Java. In fact, the implementation of the Math object in JavaScript closely parallels the Math class in Java, except that the JavaScript Math object offers fewer methods.

JavaScript's Math object properties are treated as constants. In fact, the property names are in all upper-case, following the usual convention of capitalizing variable constants. These properties return often-used values, including pi and the square root of 2. The Math methods are used in mathematical and trigonometric calculations. Handy Math-object methods include ceil, floor, pow, exp (exponent), max, min, round, and random. (Random is only available when using the X Window platform, however.)

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources