JavaScript function call
There are four kinds of JavaScript function is called.
Each different ways that this mode initialization.
this keyword
In general, in Javascript, this refers to the current object functions executed.
Note that this is a reserved keyword, you can not modify this value. |
JavaScript function call
In the previous section we have learned how to create a function.
The function code is executed after the function is called.
As a function call
Examples
return a * b;
}
myFunction (10, 2); // myFunction (10, 2) returns 20
The above function does not belong to any object. However, in JavaScript, it is always the default global object.
In HTML, the default global object is the HTML page itself, so the function is part of the HTML page.
In the browser page object browser window (window objects). Over function automatically becomes a function of window object.
myFunction () and window.myFunction () is the same:
Examples
return a * b;
}
window.myFunction (10, 2); // window.myFunction (10, 2) returns 20
This is a commonly used method invokes a JavaScript function, but it is not good programming practice global variables, methods or functions likely to cause naming conflicts bug. |
Global Objects
When a function calls itself is not an object?, This value will become the global object.
In the web browser, the browser window is the global object (window objects).
The examples of this return value is the window object:
Examples
return this;
}
myFunction (); // Returns the window object
Function as a global object to call, this will be the value of the global object. Use window object as a variable is likely to cause the program to crash. |
As a function of the method call
In JavaScript you can use a function defined as an object method.
The following example creates an object (myObject), the object has two properties (firstName and lastName), and a method (fullName):
Examples
firstName: "John",
lastName: "Doe",
fullName: function () {
return this.firstName + "" + this.lastName;
}
}
myObject.fullName (); // returns "John Doe"
fullName method is a function. Function belong to the object. myObject is the owner of the function.
this object has a JavaScript code. Value myObject object instance of this.
The following test! FullName modify this method and return value:
Examples
firstName: "John",
lastName: "Doe",
fullName: function () {
return this;
}
}
myObject.fullName (); // returns [object Object] (owner of an object)
Function as an object method call, so this will be the value of the object itself. |
Use the constructor function calls
If the function is called before using the new keyword, the constructor is called.
It looks like to create a new function, but in fact JavaScript functions are objects re-created:
Examples
function myFunction (arg1, arg2) {
this.firstName = arg1;
this.lastName = arg2;
}
// This creates a new object
var x = new myFunction ( "John", "Doe");
x.firstName; // returns "John"
Call the constructor to create a new object. New object inherits the properties and methods of the constructor.
Constructor this keyword does not have any value. this value to create an instance of an object (new object) in a function call. |
As a function of the method call function
In JavaScript, functions are objects. JavaScript function has its attributes and methods.
call () and apply () function is a predefined method. Both methods can be used to call a function, the first argument two methods must be the object itself.
Examples
return a * b;
}
myFunction.call (myObject, 10, 2); // returns 20
Examples
return a * b;
}
myArray = [10,2];
myFunction.apply (myObject, myArray); // returns 20
Both methods use the object itself as the first parameter. The difference is that the second argument: apply passed is an array of parameters, that is, a plurality of parameters combined into an array passed, and then call as a call parameter passing (from the beginning of the second argument).
In JavaScript strict mode (strict mode), the function call in the first argument will become this value, even if the parameter is not an object.
In the non-JavaScript strict mode (non-strict mode), if the value of the first parameter is null or undefined, it will use the global object instead.
This way you can set the value by call () or apply (), and calling as a new method of an object that already exists. |