Java Reference
In-Depth Information
When the
createFunction1()
creates a new function using the
Function
object,
the new function doesn't capture the local
empId
, rather it captures the global
empId
.
However, when a nested function is declared inside the
createFunction2()
function, the
new function captures the local
empId
. The output verifies this rule.
The String Object
The String object is a function. It can be used simply as a function or a constructor. When
it is used as a function, it converts the argument to a primitive type string value. The
following is an example of using
String
as a function:
var str1 = String(100); // Returns "100"
var str2 = String(); // Returns ""
When
String
is used as a constructor, it creates a new
String
object with its contents
as the passed argument:
// Create a String object
var str = new String("Hello");
Each character in the String object is assigned an index. The first character is
assigned an index of 0, the second 1, and so on. Every String object has a read-only
property named
length
that contains the number of characters in the string.
String.prototype
contains many useful methods to manipulate String objects
and get its contents in different ways. Table
4-12
lists those methods. Notice that you
can use all String object methods on a primitive string value; the primitive string will be
automatically converted to a String object before applying the method.
Table 4-12.
The List of Methods of in String.prototype with Their Descriptions
Method
Description
charAt(index)
Returns the character at the specified
index
as a
primitive string value
charCodeAt(index)
Returns the Unicode value of the character at the
specified
index
concat(arg1, arg2, ...)
Returns a primitive string concatenating the contents of
the object and the specified arguments
indexOf(substring, start)
Returns the index of the first occurrence of the substring
in the object. The search starts at
start
. If
start
is not
passed, the search starts at index 0
lastIndexOf(substring,
start)
It works the same way as the
indexOf()
method, except
that it searches for the
substring
from the end of the
string
(
continued
)
Search WWH ::
Custom Search