|
|
function [name] (arg_1, ..., arg_n)
{
statement_list
}
|
The function declaration creates a new function with formal arguments
arg_1 through arg_n and body statement_list.
If name is given, the function is stored as the value of the
variable name.
Function declarations are expressions, so they can be used as
arguments for functions like sort and
replace.
Sort with comparison function
a = [{a:19}, {a:1}, {a:13}]
a = a.sort(function(a, b) { return a.a < b.a ? -1 : 1 })
a.toSource()
|
[{a:1}, {a:13}, {a:19}]
|
Regexp replace with replace function
"abcdefa".replace(/[ac]/g,
function(a) { return a.charCodeAt(0) })
|
97b99def97
|
A function call binds the actual arguments supplied to the call to the
formal arguments in the function definition. If the caller supplies
fewer arguments than the definition asks for, Resin will bind the
value undefined . The arguments are treated as local
variables, i.e. modifying the arguments will not change any global
value. (See the var statement.)
Argument Binding
function foo(a, b) { return a + ' ' + b; }
foo(3)
|
3 undefined
|
Arguments are locals
var a = "global"
function foo(a) {
var c = a;
a = "local";
return c + ' ' + a;
}
foo("dummy") + ' ' + a
|
dummy local global
|
Function calls also set the arguments variable. This array
contains the calling arguments in call order. The arguments
variable allows variable length function calls.
Variable length arguments
function sum()
{
var sum = 0;
for (var i = 0; i < arguments.length; i++)
sum += arguments[i];
return sum;
}
sum(1, 2, 3, 4, 5)
|
15
|
The arguments variable also contains an alias to each of the
formal variables. So arguments[arg_1] returns the
same value as arg_1.
Finally, arguments contains the property callee
which contains the function itself, allowing anonymous functions to
make recursive calls.
Anonymous Fibonacci
fib = function (a) {
if (a < 2)
return 1;
else
return arguments.callee(a - 1) + arguments.callee(a - 2);
}
for (var i = 1; i <= 5; i++)
writeln(fib(i))
|
1
2
3
5
8
|
The special variable this allows functions to access
properties of the called object, i.e. it allows them to act as methods
instead of free functions. Since JavaScript functions are objects and
can be assigned dynamically, the value of this depends on how
the function is called. If it's called as a method, this is
the object. If it's called as a free function, this is the
global object.
call expression |
value of this
|
foo() |
the global object |
obj.foo() |
obj
|
obj["foo"]() |
obj
|
(expr)() |
the global object |
new foo() |
a new object with prototype foo.prototype
|
foo.call(obj) |
obj
|
foo.apply(obj) |
obj
|
Using this as class method
class Counter {
/**
* Return new value of count
*/
function next() { return this.count++; }
/**
* Create a new counter
*/
function Counter(a) { this.count = a; }
}
counter = new Counter(10);
for (var i = 0; i < 3; i++)
writeln(counter.next())
|
10
11
12
|
Scripts can declare functions inside of functions, creating closures.
The inner function, i.e. the closure, can access the outer function's
local variables as its own variables. In fact, the closure can access
the outer function's variables even when the outer function has
completed.
Closure
function outer(a)
{
return function() { return a++ } // incrementing function
}
next = outer(10); // 'next' can still access 'a'
for (var i = 0; i < 3; i++)
writeln(next());
|
10
11
12
|
Copyright © 1998-2000 Caucho Technology. All rights reserved.
Last modified: Thu, 16 Sep 1999 14:56:48 -0700 (PDT)
|