-
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: >>> String.fromCharCode(115, 99, 114, 105, 112, 116); "script" |
|
Property/Method |
Description |
|---|---|
|
|
The number of characters in the string. >>> new String('four').length
4 |
|
|
Returns the character at the specified position. Positions start at 0. >>> "script".charAt(0); "s" |
|
|
Returns the code of the character at the specified position. >>> "script".charCodeAt(0); 115 |
|
|
Return a new string glued from the input pieces. >>> "".concat('zig', '-', 'zag');
"zig-zag" |
|
|
If the needle matches a part of the string, the position of the match is returned. The optional second parameter tells where the search should start from. Returns -1 if no match is found. >>> "javascript".indexOf('scr')
4 >>> "javascript".indexOf('scr', 5)
-1 |
|
|
Same as >>> "javascript".lastIndexOf('a')
3 |
|
|
Compares two strings in the current locale. Returns 0 if the two strings are equal, 1 if the needle gets sorted before the string object, -1 otherwise. >>> "script".localeCompare('crypt')
1 >>> "script".localeCompare('sscript')
-1 >>> "script".localeCompare('script')
0 |
|
|
Accepts a regular expression object and returns an array of matches. >>> "R2-D2 and C-3PO".match(/[0-9]/g) ["2", "2", "3"] |
|
|
Allows you to replace the matching results of a regexp pattern. The replacement can also be a callback function. Capturing patterns are available as >>> "R2-D2".replace(/2/g, '-two') "R-two-D-two" >>> "R2-D2".replace(/(2)/g, '$1$1') "R22-D22" |
|
|
Returns the position of the first regular expression match. >>> "C-3PO".search(/[0-9]/) 2 |
|
|
Returns the part of a string identified by start and end position. If >>> "R2-D2 and C-3PO".slice(4,13) "2 and C-3" >>> "R2-D2 and C-3PO".slice(4,-1) "2 and C-3P" |
|
|
Turns a string into an array. The second parameter, >>> "1,2,3,4".split(',')
["1", "2", "3", "4"] >>> "1,2,3,4".split(',', 2)
["1", "2"] |
|
|
Similar to >>> "R2-D2 and C-3PO".substring(4, 13) "2 and C-3" >>> "R2-D2 and C-3PO".substring(13, 4) "2 and C-3" |
|
|
Transforms the string to lower case. >>> "JAVA".toLowerCase() "java" |
|
|
Transforms the string to upper case. >>> "script".toUpperCase() "SCRIPT" |
Change the font size
Change margin width
Change background colour