function f(parameter1, paremeter2) { // define function
argument1 = parameter1; // pass parameters in as arguments
argument2 = parameter2;
return argument1 + argument2;
}
f() // initialize call object and invoke funciton
argument is the value/variable/reference being passed in, parameter is the receiving variable used w/in the function/block.
Function parameters are the names listed in the function definition.
Function arguments are the real values received by the function when it is invoked.
apply()
is like call()
method, except that apply(object,[arg1, arg2])
takes its arguments in an array (second object in the arguments)
When JavaScript reaches a return statement, the function will stop executing.
Functions in JavaScript run in the scope in which they are defined, not the scope in which they are excuted.
When a function is defined, a "scope chain" is saved and becomes the internal state of the function.
At the top level, it inherits the global object.
When a nested function is defined, the scope chain includes the containing function; which means (as Terry pointed out), the nested functions can access all of the arugments and local variables of the containing fucntion.
- sets the scope to the scope chain
- add call objects to the front of the scope chain with initialized property named arguments (referring to the arugment objects for the function)
- Named parameters are added to the call objects (argument objects) and local variables (var statements) are also defined withing the call objects (as keywords to the call object; they are not proproties of the call object)
Call objects are often used as a Namespace to keep variables from currupting the global namespace. This way the code only adds the function as a single property to the global namespace.
a function with no names.
(function() {
})(); //end of function literal and invoke it now!
the parentheses around the function literal are reuqired by Javascript syntax.
var x = "global";
function f() {
var x = "local";
function g() {
alert(x);
}
g();
}
f(); // calling this displays "local"
functions are data just like any other value.
When the function is invoked, a call object is created for it and placed on the scope chain. When the function exits, the call object is removed from the scope chain. When the object is removed from the chain, there are no more references to it, and it ends up being garbage collected.
Nested function changes that. If a nested function is created, the definition of that function refers to the call objects because that call object is the top fo the scope chain in which the function is defined.
If the nested function is used only within the containing function (the call object), when the containing function exits, they all go into garbage collection.
If you save a reference to the nested function in the global scope—by using the nested function as the return value of the containing function or storing the nested function as the property of other objects—which creates an "external" reference to the nested function, then the nested function retains its reference to the call object of the containing function. Therefore the names and values of the function arguments and local variables persist to live in this object.
This combination of code and scope is called closure. These closures are only interesting when a nested function is exported outside the scope in which it is defined.