Basic Scenario
When a function is defined, the ()
area is used for inputs. These inputs are mapped to what data is sent in.
function newFunction(data, status){
}
newFunction(1,2);
In this scenario, data
will be assigned the value of 1
, and status
will be assigned the value of 2
for the scope of newFunction
.
Missmatched inputs
However, it does not always directly map. If fewer arguments are sent, then the unassigned input variables become undefined
.
function newFunction(data, status){
}
newFunction(1);
In this scenario, data
will be assigned the value of 1
, and status
will be assigned the value of undefined
for the scope of newFunction
.
arguments object
Inside of the scope of newFunction
, there is also access to the array like object called arguments
.
function newFunction(data, status)
{
var args = arguments;
}
newFunction(1);
In this scenario, the variable args
will hold the arguments
object. There you can check args.length
to see that only 1 argument was sent. args[0]
will give you that argument value, being 1
in this case.
Function object
A function can be made into an object with the new
keyword. By using the new
keyword on a function, a Function object is made. Once the Function object is made, this
may be used to refer to the current Function object instead of the global window.
function newFunction(data,status){
if( this instanceof newFunction ){
//use as a Function object
this.data = data;
}else{
//use as a normal function
}
}
var myNewFunction = new newFunction(1);
In this scenario, myNewFunction
now holds a reference to a Function object, and can be accessed as an object through dot notation or indexing. myNewFunction["data"]
and myNewFunction.data
now both hold the value of 1.