Section 3.13. Primitive Datatype Wrapper Objects


3.13. Primitive Datatype Wrapper Objects

When we discussed strings earlier in this chapter, I pointed out a strange feature of that datatype: to operate on strings, you use object notation.[*] For example, a typical operation involving strings might look like the following:

[*] This section covers advanced material, which you may want to skip on your first reading.

 var s = "These are the times that try people's souls."; var last_word = s.substring(s.lastIndexOf(" ")+1, s.length); 

If you didn't know better, it would appear that s was an object and that you were invoking methods and reading property values of that object.

What's going on? Are strings objects, or are they primitive datatypes? The typeof operator (see Chapter 5) assures us that strings have the datatype "string", which is distinct from the datatype "object". Why, then, are strings manipulated using object notation?

The truth is that a corresponding object class is defined for each of the three key primitive datatypes. That is, besides supporting the number, string, and boolean datatypes, JavaScript also supports Number, String, and Boolean classes. These classes are wrappers around the primitive datatypes. A wrapper contains the same primitive data value, but it also defines properties and methods that can be used to manipulate that data.

JavaScript can flexibly convert values from one type to another. When you use a string in an object contexti.e., when you try to access a property or method of the stringJavaScript internally creates a String wrapper object for the string value. This String object is used in place of the primitive string value. The object has properties and methods defined, so the use of the primitive value in an object context succeeds. The same is true, of course, for the other primitive types and their corresponding wrapper objects; you just don't use the other types in an object context nearly as often as you use strings in that context.

Note that the String object created when you use a string in an object context is a transient one; it allows you to access a property or method, and then it is no longer needed, so it is reclaimed by the system. Suppose s is a string and the length of the string is determined with a line like this:

 var len = s.length; 

In this case, s remains a string; the original string value itself is not changed. A new transient String object is created, which allows you to access the length property, and then the transient object is discarded, with no change to the original value s. If you think this scheme sounds elegant and bizarrely complex at the same time, you are right. Typically, however, JavaScript implementations perform this internal conversion very efficiently, and it is not something you should worry about.

If you want to use a String object explicitly in your program, you have to create a nontransient one that is not automatically discarded by the system. String objects are created just like other objects, with the new operator. For example:

 var s = "hello world";              // A primitive string value var S = new String("Hello World");  // A String object 

Once you've created a String object S, what can you do with it? Nothing that can't be done with the corresponding primitive string value. If you use the typeof operator, it says that S is indeed an object and not a string value, but except for that case, you'll find that you can't normally distinguish between a primitive string and the String object.[*] As I've already shown, strings are automatically converted to String objects whenever necessary. It turns out that the reverse is also true. Whenever you use a String object where a primitive string value is expected, JavaScript automatically converts the String to a string. So if you use a String object with the + operator, a transient primitive string value is created so that the string concatenation operation can be performed:

[*] Note, however, that the eval( ) method treats string values and String objects differently, and it will not behave as you expect it to if you inadvertently pass it a String object instead of a primitive string value.

 msg = S + '!'; 

Bear in mind that everything discussed in this section about string values and String objects applies also to number and boolean values, and their corresponding Number and Boolean objects. You can learn more about the Number and Boolean classes from their respective entries in Part III.

Finally, note that any number, string, or boolean value can be converted to its corresponding wrapper object with the Object( ) function:

 var number_wrapper = Object(3); 




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net