INVOCATION OF FUNCTIONS
Code within a JavaScript function will be executed when “something” invokes it. The code is not executed when the function is defined but when the function is called. It is common to use the term “calling a function” instead of “calling a function.” It is also common to say “call up a function,” “start a function,” or “execute a function.” In this tutorial we will use invoke, because a JavaScript function can be invoked without being called.
function myFunction(a, b) {
return a * b;
}
myFunction(10, 2); // Will return 20
The function above does not belong to any object. But in JavaScript there is always a default global object. In HTML, the default global object is the HTML page itself; therefore, the function above “belongs” to the HTML page. In a browser the page object is the browser window. The function above automatically becomes a function of the window object.
Note
This is a common way to invoke a JavaScript function, but it is not a very good practice. Global variables, methods or functions can easily create name conflicts and bugs in the global object. myFunction() and window.myFunction() are the same function:
function myFunction(a, b) {
return a * b;
}
window.myFunction(10, 2); // Will also return 20
THE GLOBAL OBJECT
When a function is called without a proprietary object, the value of this becomes the global object. In a Web browser, the global object is the browser window. This example returns the window object as the value of this:
let x = myFunction(); // x will be the window object
function myFunction() {
return this;
}
Invoking a function as a global function causes the value of this to be the global object.
Using the window object as a variable can easily cause the program to crash.
INVOKE A FUNCTION AS A METHOD
In JavaScript you can define functions as object methods. The following example creates an object (myObject), with two properties (firstName and lastName) and a method (fullName):
const myObject = {
firstName:“John”,
lastName: “Doe”,
fullName: function () {
return this.firstName + ” ” + this.lastName;
}
}
myObject.fullName(); // Will return “John Doe”
The fullName method is a function. The function belongs to the object. myObject is the owner of the function. This is the object that “owns” the JavaScript code. In this case the value of this is myObject. Try it out! Modify the fullName method to return the value of this:
const myObject = {
firstName:“John”,
lastName: “Doe”,
fullName: function () {
return this;
}
}
myObject.fullName();
// This will return [object Object] (the owner object)
If a function call is preceded by the keyword new, it is a constructor call. It looks like you create a new function, but since JavaScript functions are objects, you actually create a new object:
// This is a function constructor:
function myFunction(arg1, arg2) {
this.firstName = arg1;
this.lastName = arg2;
}
// This creates a new object
const myObj = new myFunction(“John”, “Doe”);
// This will return “John”
myObj.firstName;
The keyword this in the constructor does not have a value. The value of this will be the new object created when the function is called.
WHAT IS THIS?
In JavaScript, the this keyword refers to an object. Which object depends on how it is invoked (used or called). The keyword this refers to different objects depending on how it is used:
Leave A Comment