13. When Primitives Behave Like Objects


In This Chapter

Get a deeper understanding of how primitives and objects work

Understand that even primitives have object-like traits

Wonder how JavaScript ever got to being so popular


In the previous chapter (Chapter 12, “Strings) and less so in the Of Pizza, Types, Primitives, and Objects chapter (Chapter 11), you got a sneak peek at something that is probably pretty confusing. I’ve stated many times that primitives are very plain and simple. Unlike Objects, they don’t contain properties that allow you to fiddle with their values in interesting (or boring) ways. Yet, as clearly demonstrated by all the stuff you can do with strings, your primitives seem to have a mysterious dark side to them:

var greeting = "Hi, everybody!!!";
var shout = greeting.toUpperCase(); // where did toUpperCase
  come from?

As you can see from this brief snippet, your greeting variable, which stores a primitive value in the form of text, seems to have access to the toUpperCase method. How is this even possible? Where did that method come from? Why am I here? Answers to confusing existential questions like this will make up the bulk of what you see in this page. Also, I apologize for writing that previous sentence in passive voice. Happen again it won’t.

Strings Aren’t the Only Problem

Because of how cute they are, it’s easy to pick on strings as the main perpetrator of this primitive/Object confusion. As it turns out, many of the built-in primitive types are involved in this racket as well. Table 13.1 presents the built-in Object types with the guilty parties that also exist as primitives highlighted.

Image

TABLE 13.1 Object Types that also Exist as Primitives

Whenever you are working with boolean, number, or string primitives, you have access to properties their Object equivalent exposes. In the following sections, you’ll see what exactly is going on.

Let’s Pick on Strings Anyway

Just as you were taught by your parents growing up, you typically use a string in the literal form:

var primitiveText = "Homer Simpson";

As you saw in the table earlier, strings also have the ability to be used as objects. There are several ways to create a new object, but the most common way to create an object for a built-in type like our string is to use the new keyword followed by the word String:

var name = new String("Homer Simpson");

The String in this case isn’t just any normal word. It represents what is known as a constructor function whose sole purpose is to be used for creating objects. Just like there are several ways to create objects, there are several ways to create String objects as well. The way I see it, knowing about one way that you really shouldn’t be creating them with is enough.

Anyway, the main difference between the primitive and object forms of a string is the sheer amount of additional baggage the object form carries with it. Let’s bring our silly visualizations back. Your primitiveText variable and its baggage looks as shown in Figure 13.1.

Image

FIGURE 13.1 What primitiveText looks like.

There really isn’t much there. Now, don’t let the next part scare you, but if we had to visualize our String object called name, it would look like what you see in Figure 13.2.

Image

FIGURE 13.2 It is a bit more complex, right?

You have your name variable containing a pointer to the text, Homer Simpson. You also have all of the various properties and methods that go with the String object - things you may have used like indexOf, toUpperCase, and so on. You’ll get a massive overview of what exactly this diagram represents in Chapter 16, “A Deeper Look at Objects,” so don’t worry yourself too much about what you see here. Just know that the object form of any of the primitives carries with it a lot of functionality.

Why This Matters

Let’s go back to our earlier point of confusion. Our string is a primitive. How can a primitive type allow you to access properties on it? The answer has to do with JavaScript being really weird. Let’s say you have the following string:

var game = "Dragon Age: Origins";

The game variable is very clearly a string primitive that is assigned to some literal text. If I want to access the length of this text, I would do something as follows:

var game = "Dragon Age: Origins";
alert(game.length);

As part of evaluating game.length, JavaScript will convert your primitive string into an object. For a brief moment, your lowly primitive will become a beautiful object to figure out what the length actually is. The thing to keep in mind is that all of this is temporary. Because this temporary object isn’t grounded or tied to anything after it serves its purpose, it goes away and you are left with the result of the length evaluation (a number) and the game variable (still a string primitive).

This transformation only happens for primitives. If you ever explicitly create a String object, then what you create is permanently kept as an object. Let’s say you have the following:

var gameObject = new String("Dragon Age:Origins");

In this case, our gameObject variable very clearly points to something whose type is Object. This variable will continue to point to an Object type unless you modify the string or do something else that causes the reference to be changed. The primitive morphing into an object and then morphing back into a primitive is something unique to primitives. Your objects don’t partake in such tomfoolery.

You can easily verify everything I’ve said by examining the type of your data. That is done by using the typeof keyword. Here is an example of me using it to confirm everything I’ve just told you about:

var game = "Dragon Age: Origins";
alert("Length is: " + game.length);

var gameObject = new String("Dragon Age:Origins");

alert(typeof game); // string
alert(typeof game.length); // number
alert(typeof gameObject); // Object

Now, aren’t you glad you learned all this?


Image Tip

Just a quick reminder for those of you reading these words in the print or e-book edition of this book: If you go to www.quepublishing.com and register this book, you can receive free access to an online Web Edition that not only contains the complete text of this book but also features a short, fun interactive quiz to test your understanding of the chapter you just read.

If you’re reading these words in the Web Edition already and want to try your hand at the quiz, then you’re in luck – all you need to do is scroll down!


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.97.40