The best JavaScript function call Tutorial In 2024, In this tutorial you can learn this keyword,JavaScript function call,As a function call,Examples,Examples,Global Objects,Examples,As a function of the method call,Examples,Examples,Use the constructor function calls,Examples,As a function of the method call function,Examples,Examples,
There are four kinds of JavaScript function is called.
Each different ways that this mode initialization.
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. |
In the previous section we have learned how to create a function.
The function code is executed after the function is called.
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:
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. |
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:
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. |
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):
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:
Function as an object method call, so this will be the value of the object itself. |
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:
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. |
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.
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. |