Page 112 - DCAP202_Fundamentals of Web Programming
P. 112
Unit 8: Programming Constructs in JavaScript
8.3.5 Functions Properties Notes
Functions in JavaScript are actually an object. Therefore any created function created using the
“function” declaration will have the properties of a function. These properties are:
arguments - An array of arguments passed to the function. This is an array object which
has a property of length which enables the programmer to tell how many arguments (or
variables) are passed to the function.
caller - The name of the function that called the function.
prototype - Used to make more properties.
8.3.6 Using the Arguments Array
The arguments object is a local variable available within all functions; arguments as a property
of Function can no longer be used.
You can refer to a function’s arguments within the function by using the arguments object. This
object contains an entry for each argument passed to the function, the first entry’s index starting
at 0.
For example, if a function is passed three arguments, you can refer to the argument as follows:
arguments[0]
arguments[1]
arguments[2]
The arguments can also be set:
arguments[1] = ‘new value’;
The arguments object is not an Array. It is similar to an Array, but does not have any Array
properties except length.
Example: It does not have the pop method. However it can be converted to a real Array:
var args = Array.prototype.slice.call(arguments);
If Array generics are available, one can use the following instead:
var args = Array.slice(arguments);
The arguments object is available only within a function body. Attempting to access the
arguments object outside a function declaration results in an error.
You can use the arguments object if you call a function with more arguments than it is formally
declared to accept. This technique is useful for functions that can be passed a variable number of
arguments. You can use arguments.length to determine the number of arguments passed to the
function, and then process each argument by using the arguments object. (To determine the
number of arguments declared when a function was defined, use the Function.length property.)
8.3.7 Local Variables
Local variables exist only inside a particular function hence they have Local Scope. Global variables
on the other hand are present throughout the script and their values can be accessed by any
function. Thus, they have Global Scope.
LOVELY PROFESSIONAL UNIVERSITY 105