loading...

JavaScript – Wrapper Objects

JavaScript objects are composite values: they are a collection
of properties or named values. We refer to the value of a property
using the . notation. When the
value of a property is a function, we call it a
method. To invoke the method m of an object o, we write o.m().

We’ve also seen that strings have properties and
methods:

var s = "hello world!";                             // A string
var word = s.substring(s.indexOf(" ")+1, s.length); // Use string properties

Strings are not objects, though, so why do they have properties?
Whenever you try to refer to a property of a string s, JavaScript converts the string value to
an object as if by calling new
String(s)
. This object inherits (see Inheritance) string methods and is used to resolve the
property reference. Once the property has been resolved, the newly
created object is discarded. (Implementations are not required to
actually create and discard this transient object: they must behave as
if they do, however.)

Numbers and booleans have methods for the same reason that
strings do: a temporary object is created using the Number() or Boolean() constructor, and the method is
resolved using that temporary object. There are not wrapper objects
for the null and undefined values: any attempt to access a
property of one of these values causes a TypeError.

Consider the following code and think about what happens when it
is executed:

var s = "test";         // Start with a string value.
s.len = 4;              // Set a property on it.
var t = s.len;          // Now query the property.

When you run this code, the value of t is undefined. The second line of code creates a
temporary String object, sets its len property to 4, and then discards that
object. The third line creates a new String object from the original
(unmodified) string value and then tries to read the len property. This property does not exist,
and the expression evaluates to undefined. This code demonstrates that
strings, numbers, and boolean values behave like objects when you try
to read the value of a property (or method) from them. But if you
attempt to set the value of a property, that attempt is silently
ignored: the change is made on a temporary object and does not
persist.

The temporary objects created when you access a property of a
string, number, or boolean are known as wrapper
objects
, and it may occasionally be necessary to
distinguish a string value from a String object or a number or boolean
value from a Number or Boolean object. Usually, however, wrapper
objects can be considered an implementation detail and you don’t have
to think about them. You just need to know that string, number, and
boolean values differ from objects in that their properties are
read-only and that you can’t define new properties on them.

Note that it is possible (but almost never necessary or useful)
to explicitly create wrapper objects, by invoking the String(), Number(), or Boolean() constructors:

var s = "test", n = 1, b = true;  // A string, number, and boolean value.
var S = new String(s);            // A String object
var N = new Number(n);            // A Number object
var B = new Boolean(b);           // A Boolean object

JavaScript converts wrapper objects into the wrapped primitive
value as necessary, so the objects S, N, and
B above usually, but not always,
behave just like the values s,
n, and b. The ==
equality operator treats a value and its wrapper object as equal, but
you can distinguish them with the === strict equality operator. The typeof operator will also show you the
difference between a primitive value and its wrapper object.

Comments are closed.

loading...