Java Reference
In-Depth Information
If the function has arguments, these are given in the same order after the first argument:
function greet(greeting){
return greeting + " " + this.name;
}
greet.call(alfie, "Hello");
<< "Hello Alfie"
greet.call(betty, "Yo");
<< "Yo Betty"
If a function does not refer to an object as this in its body, null can be provided as the
first argument:
square.call(null, 4)
<< 16
The apply method works in the same way, except the arguments of the function are
provided as an array, even if there is only one argument:
square.apply(null, [4])
<< 16
This can be useful if the data you're using as an argument is already in the form of an array.
These two methods allow generalized functions to be written that are not tied to specific
objects by being methods of that object, giving more flexibility over the usage of the func-
tion.
Custom Properties
There is nothing to stop from you adding your own properties to functions. For example,
you could add a description property to a function that describes what it does:
square.description = "Squares a number that is provided as
an
Search WWH ::




Custom Search