↑
Main Page
instanceof operator
alert(oStringObject.substring(-3)); //outputs “hello world”
alert(oStringObject.slice(3, -4)); //outputs “lo w”
alert(oStringObject.substring(3,-4)); //outputs “hel”
Here, you see the main difference between
slice()
and
substring()
. When you call each with one
argument,
-3
,
slice()
returns
“rld”
while
substring()
returns
“hello world”
. This occurs
because
slice(-3)
translates into
slice(7)
for the string
“hello world”
whereas
substring(-3)
translates into
substring(0)
. Likewise, the difference is apparent when using the parameters
3
and
–4
.
For the
slice()
method, this translates into
slice(3,7)
, the same as the previous example, which
returns
“lo w”
as the result. However the
substring()
method interprets this as
substring(3,0)
,
which is essentially
substring(0, 3)
because
substring()
always considers the smaller number as
the start and the larger number as the end. As a result,
substring(3,-4)
returns
“hel”
. The bottom
line here is to be clear about how you are using these two methods.
The last set of methods to be discussed involves case conversion. Four methods perform case conver-
sion:
toLowerCase()
,
toLocaleLowerCase()
,
toUpperCase()
, and
toLocaleUpperCase()
. The
uses for these methods are pretty obvious from their names — two convert the string into all lowercase
and two convert the string into all uppercase. The
toLowerCase()
and
toUpperCase()
methods are
the originals, modeled after the same methods in
java.lang.String
; the
toLocaleLowerCase()
and
toLocaleUpperCase()
methods are intended to be implemented based on a particular locale (in the
same way
localeCompare()
is intended to be used). In many locales, the locale-specific methods are
identical to the generic ones; however, a few languages apply special rules to Unicode case conversion
(such as Turkish), and this necessitates using the locale-specific methods for proper conversion.
var oStringObject= new String(“Hello World”);
alert(oStringObject.toLocaleUpperCase()); //outputs “HELLO WORLD”
alert(oStringObject.toUpperCase()); //outputs “HELLO WORLD”
alert(oStringObject.toLocaleLowerCase()); //outputs “hello world”
alert(oStringObject.toLowerCase()); //outputs “hello world”
This code outputs
“HELLO WORLD”
for both
toLocaleUpperCase()
and
toUpperCase()
, just as
“hello world”
is output for both
toLocaleLowerCase()
and
toLowerCase()
. Generally speaking,
if you do not know the language in which the code will be running, it is safer to use the locale-specific
methods.
Remember, all the methods and properties for the
String
class also apply to String primitive values
because they are pseudo-objects.
The instanceof operator
One of the problems with using reference types to store values has been the use of the
typeof
operator,
which returns
“object”
no matter what type of object is being referenced. To provide a solution,
ECMAScript introduced another Java operator:
instanceof
.
The
instanceof
operator works in a similar way to the
typeof
operator: It identifies the type of object
you are working with. Unlike
typeof
,
instanceof
requires the developer to explicitly ask if an object is
of a particular type. For example:
var oStringObject = new String(“hello world”);
alert(oStringObject instanceof String); //outputs “true”
32
Chapter 2
05_579088 ch02.qxd 3/28/05 11:35 AM Page 32
Free JavaScript Editor
Ajax Editor
©
→