Chapter 8
When a function is invoked on an object, the function is called a method, and the object on which it is invoked is passed as an implicit argument of the function.
JavaScript supports quite a few built-in functions, such as eval(), parseInt().
But, which object do the function belong to??
Section 8.1
Functions may be defined to expect varying numbers of arguments
If the return statement does not have an associated expression, it returns the undefined value
If a function does not contain a return statement, it returns the undefined value
Note that parameters of a function are normally available only within the body of the executing function: they cannot be accessed outside the body of the function or after the function has returned. (But see Section 8.8 for an important exception.)
JavaScript does not check whether you have passed the type of data that the function expects, JavaScript does not check whether you have passed the correct number of arguments either.
A function accesses the arguments by their position in the argument list rather than by name.
Nested functions may be defined only at the top level of the function within which they are nested. That is, they may not be defined within statement blocks, such as the body of an if statement of while loop.
function f(x) { return x*x; } // function statement var f = function(x) { return x*x; }; // function literal |
A function literal is an expression that defines an unnamed function.
The syntax for function literal is an expression, while statement for function
Although function literals create unnamed functions, the syntax allows a function name to be optionally specified, which is useful when writing recursive functions that call themselves, e.g. :
var f = function fact(x) { if (x <= 1) return 1; else return x*fact(x-1); }; |
1. Define an unnamed function
2. The reference to this function is stored in the variable f rather than fact
3. The function body is allowed to refer to fact
Function literals are created by JavaScript expression rather than statements
The function specified by a function literal expression can be stored into a variable, passed to another function, or even invoked directly:
f[0] = function(x) { return x*x; }; // Define a function and store it a.sort(function(a,b){return a-b;}); // Define a function; pass it to another var tensquared = (function(x) {return x*x;})(10); // Define and invoke |
Recall from Chapter 2 that dollar signs and underscores ($ _ ) are the two characters besides letters and numbers that are legal in JavaScript identifiers.
Chapter 8.2
JavaScript functions can be invoked with any number of arguments, regardless of the number of arguments named in the function definition.
It’s legal to pass values of any type to any function, because a function is loosely typed, there is no way for it to declare the type of arguments it expects.
a = a || []; |
Recall from Chapter5 that the || operator returns its first argument if the argument is true or value that converts to true, otherwise, it returns its second argument.
Arguments object is an array-like object that allows the argument values passed to the function to be retrieved by number, rather than by name, and it also defines an additional callee property.
Generally,
function test() var abc = "I'm abc"; var arrayTest = [abc,'xyz']; abc = "abc has been changed";
alert(arrayTest[0]); // I'm abc alert(abc); // abc has been changed alert(arrayTest[1]); // xyz } |
But if arguments is referred, it is another case
function f(x) { alert(x); // Displays the initial value of the argument arguments[0] = null; // Changing the array element also changes x! alert(x); // Now displays "null" } |
Finally, bear in mind that arguments is just an ordinary JavaScript identifier, not a reserved word. If a function has an argument or local variable with that name, it hides the reference to the Arguments object. For this reason, it is good idea to treat arguments as a reserved word and avoiding using it as a variable name.
arguments has a property called callee, which refers to the function itself.
Since JavaScript is loosely typed, method arguments have no declared types, and no type checking is performed on the values passed to a function.