JavaScript Function Invocation
JavaScript functions have 4 ways of being called.
Each way differs in the this initialization of this.
this The keyword
Generally speaking, in Javascript, this points to the current object when the function is executed.
![]() |
Note this is a reserved keyword; you cannot modify this value. |
|---|
Calling JavaScript Functions
In previous chapters we have learned how to create a function.
The code in a function is executed after the function is called.
Calling a Function as a Function
Example
function
myFunction
(
a
,
b
)
{
return
a
b
;
}
myFunction
(
10
,
2
)
;
//
myFunction(10, 2) returns 20
Try it »
The above function does not belong to any object. But in JavaScript it is always the default global object.
In HTML, the default global object is the HTML page itself, so the function belongs to the HTML page.
The page object in a browser is the browser window (window object). The above function automatically becomes a function of the window object.
myFunction() and window.myFunction() are the same:
Example
function
myFunction
(
a
,
b
)
{
return
a
b
;
}
window
.
myFunction
(
10
,
2
)
;
//
window.myFunction(10, 2) returns 20
Try it »
|
| This is a common way to call a JavaScript function, but it is not a good programming practice
Global variables, methods, or functions can easily cause bugs with naming conflicts. | | --- | --- |
The Global Object
When a function is not called by its own object, this the value of this becomes the global object.
In a web browser, the global object is the browser window (window object).
This example returns this the value of this is the window object:
Example
function
myFunction
(
)
{
return
this
;
}
myFunction
(
)
;
//
Returns the window object
Try it »
|
| Calling a function as a global object causes this the value of this to become the global object.
Using the window object as a variable can easily cause the program to crash. | | --- | --- |
Calling a Function as a Method
In JavaScript you can define a function as a method of an object.
The following example creates an object (myObject), the object has two properties (firstName and lastName), and one method (fullName):
Example
var
myObject
=
{
firstName
:
"
John
"
,
lastName
:
"
Doe
"
,
fullName
:
function
(
)
{
return
this
.
firstName
"
"
this
.
lastName
;
}
}
myObject
.
fullName
(
)
;
//
returns "John Doe"
Try it »
fullName A method is a function. The function belongs to the object. myObject is the owner of the function.
thisobject, which owns the JavaScript code. In the example, this has a value of myObject object.
Test it out! Modify fullName the method and return this values:
Example
var
myObject
=
{
firstName
:
"
John
"
,
lastName
:
"
Doe
"
,
fullName
:
function
(
)
{
return
this
;
}
}
myObject
.
fullName
(
)
;
//
Returns [object Object] (the owner object)
Try it »
![]() |
Calling a function as an object method causes this the value of this to become the object itself. |
|---|
Calling a Function with a Constructor
If a function call is preceded by the new keyword, then a constructor is being called.
This looks like creating a new function, but in fact a JavaScript function is a recreated object:
Example
//
Constructor:
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"
Try it »
Calling a constructor creates a new object. The new object inherits the properties and methods of the constructor.
|
| In a constructor, the this keyword does not have any value.
this the value of this is created when the function call instantiates an object (new object). | | --- | --- |
Calling a Function as a Function Method
In JavaScript, functions are objects. JavaScript functions have their properties and methods.
call() and apply() are predefined function methods. Both methods can be used to call a function, and the first argument of both methods must be the object itself.
Example
function
myFunction
(
a
,
b
)
{
return
a
b
;
}
myObject
=
myFunction
.
call
(
myObject
,
10
,
2
)
;
//
Returns 20
Try it »
Example
function
myFunction
(
a
,
b
)
{
return
a
b
;
}
myArray
=
[
10
,
2
]
;
myObject
=
myFunction
.
apply
(
myObject
,
myArray
)
;
//
Returns 20
Try it »
Both methods use the object itself as the first argument. The difference between the two lies in the second argument: apply takes an array of arguments, that is, it combines multiple arguments into one array and passes it in, while call takes the arguments as parameters of call (starting from the second argument).
In JavaScript strict mode, when calling a function the first argument becomes this the value of this, even if the argument is not an object.
In JavaScript non-strict mode, if the value of the first argument is null or undefined, it will be replaced by the global object.
![]() |
With the call() or apply() method you can set this the value of this, and call it as a new method of an existing object. |
|---|