Java Reference
In-Depth Information
Notice also that the example ends with a semicolon. This finishes the assignment statement,
whereas a normal function declaration ends in a block (no need for semicolons at the end
of blocks).
Note: A Function's name Property
Functions are just objects, and objects have properties (more about this in
the Chapter 5 ) . All functions haves a read-only property called name , which
can be accessed like so:
<< "hello"
The name property is not actually part of the ECMAScript standard, al-
though most JavaScript engines support it and use it internally.
Anonymous functions have an empty string as their name property in most
browsers, although some versions of Internet Explorer use undefined .
The name property can be useful when debugging code, as the name of a
function will be used to indicate which functions are causing a problem.
Function() Constructors
A function can also be declared using the constructor Function() . The body of the func-
tion is entered as a string, as shown in this example:
hi = new Function('alert("Hi World!");');
We'd avoid recommending this way of declaring functions as it is slower and there are
problems with placing the function's code inside a string. Even in this simple example, we
had to use different quotation marks for the alert function as those used for defining the
function body itself.
A ninja should always declare functions using function literals―function declarations or
function expressions. These two ways of creating functions are similar, although there are
some subtle differences that will be covered later in the chapter. Some people prefer func-
Search WWH ::

Custom Search