
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
202
|
Chapter 9: Functions
Obviously we can count down without using recursive anonymous functions. We’ll
see a more realistic example of function recursion in the section on “Recursive Func-
tions” later in this chapter. Using
arguments.callee to refer to the current function
(rather than the function’s actual name) alleviates the need to update our code if the
function’s name changes.
We can use
arguments.caller to execute or identify a calling function, as described
in the Language Reference.
Passing Parameters by Value Versus by Reference
There’s one more parameter subtlety we should consider—the difference between
passing data to a function by value and by reference.
When we pass a primitive data value as an argument to a function, the function
receives a copy of the data, not the original. Changes made to a parameter in the
function have no effect on the original argument outside that function. In
Example 9-5,
variableName’s value is initially set to 25. Changing its value to 10
within the setValue() function has no effect on
y’s value.
Primitive data is therefore said to be passed by value. When we pass composite data
as an argument to a function, however, the function receives a reference that points
to the same data as the original argument, not just a duplicate of the data. Altering ...