Java Reference
In-Depth Information
Function Properties and Methods
The fact that functions are first-class objects means that they can have properties and meth-
ods themselves. For example, all functions have a length property that returns the number
of parameters the function has. In the following code, the square function takes one para-
meter:
square.length
<< 1
Call and Apply Methods
The call() method allows a function to be called by an object that is provided as the first
argument. Inside the body of the function, the keyword this is used to refer to the object
the function is called on.
In this example, the sayHi() function refers to an unspecific object called this that has
a property called name :
function sayHi(){
return "Hi " + this.name;
}
We can create some objects that have a name property and then use the call() method
to invoke the sayHi() function, providing an object as an argument. This object will then
take the value of this in the function:
alfie = { name: "Alfie" };
betty = { name: "Betty" };
sayHi.call(alfie);
<< "Hi Alfie"
sayHi.call(betty);
<< "Hi Betty"
Search WWH ::




Custom Search