-
Book Overview & Buying
-
Table Of Contents
Object-Oriented JavaScript
The String() constructor creates string objects. Primitive strings are turned into objects behind the scenes if you call a method on them as if they were objects.
Creating a string object and a string primitive:
>>> var s_obj = new String('something');
>>> var s_prim = 'something';
>>> typeof s_obj"object"
>>> typeof s_prim
"string"
The object and the primitive are not equal when compared by type with ===:
>>> s_obj === s_prim
false
>>> s_obj == s_prim
true
length is a property of string objects:
>>> s_obj.length
9
If you access length on a non-object but a primitive string, the primitive is converted to an object behind the scenes and the operation is successful:
>>> "something".length
9
|
Property/Method |
Description |
|---|---|
|
|
Returns a string created using the input character codes: >>... |
Change the font size
Change margin width
Change background colour