- JavaScript Introduction
- JavaScript Where To
- JavaScript Output
- JavaScript Statements
- JavaScript Syntax
- JavaScript Comments
- JavaScript Variables
- JavaScript Let
- JavaScript Const
- JavaScript Operators
- JavaScript Assignment
- JavaScript Data Types
- JavaScript Functions
- JavaScript Objects
- JavaScript Events
- JavaScript Strings
- JavaScript String Methods
- JavaScript Numbers
- JavaScript Number Methods
- JavaScript Arrays
- JavaScript Array Const
- JavaScript Array Methods
- JavaScript Sorting Arrays
- JavaScript Array Iteration
- JavaScript Date Objects
- JavaScript Date Formats
- JavaScript Get Date Methods
- JavaScript Set Date Methods
- JavaScript Math Object
- JavaScript Random
- JavaScript Booleans
- JavaScript Comparison And Logical Operators
- JavaScript If Else And Else If
- JavaScript Switch Statement
- JavaScript For Loop
- JavaScript Break And Continue
- JavaScript Type Conversion
- JavaScript Bitwise Operations
- JavaScript Regular Expressions
- JavaScript Errors
- JavaScript Scope
- JavaScript Hoisting
- JavaScript Use Strict
- The JavaScript This Keyword
- JavaScript Arrow Function
- JavaScript Classes
- JavaScript JSON
- JavaScript Debugging
- JavaScript Style Guide
- JavaScript Common Mistakes
- JavaScript Performance
- JavaScript Reserved Words
- JavaScript Versions
- JavaScript History
- JavaScript Forms
- JavaScript Validation API
- JavaScript Objects
- JavaScript Object Properties
- JavaScript Function Definitions
- JavaScript Function Parameters
- JavaScript Function Invocation
- JavaScript Closures
- JavaScript Classes
- Java Script Async
- JavaScript HTML DOM
- The Browser Object Model
- JS Ajax
- JavaScript JSON
- JavaScript Web APIs
- JS Vs JQuery
JavaScript Function Invocation
Invoking A JavaScript Function
The code inside a JavaScript function
will execute when "something" invokes it.
The code inside a function is not executed when the function is defined.
The code inside a function is executed when the function is invoked.
It is common to use the term "call a function" instead of "invoke a function".
It is also common to say "call upon 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.
Invoking a Function as a Function
Example
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, so the function above "belongs" to the HTML page.
In a browser the page object is the browser window. The function above automatically becomes a window function.
myFunction() and window.myFunction() is the same function:
Example
return a * b;
}
window.myFunction(10, 2); // Will also return 20
This is a common way to invoke a JavaScript function, but not a very good practice.
Global variables, methods, or functions can easily create name conflicts and bugs in the global object.
The this Keyword
In JavaScript, the thing called this
, is the object that "owns" the current code.
The value of this
, when used in a function, is the object that "owns" the function.
Note that this
is not a variable. It is a keyword. You cannot change the value of this
.
The Global Object
When a function is called without an owner 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
:
Example
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 crash your program.
Practice Excercise Practice now
Invoking 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):
Example
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.
The thing called this
, is the object that "owns" the JavaScript code. In this case the value of this
is myObject.
Test it! Change the fullName method to return the value of this
:
Example
firstName:"John",
lastName: "Doe",
fullName: function () {
return this;
}
}
// This will return [object Object] (the owner object)
myObject.fullName();
Invoking a function as an object method, causes the value of this
to be the object itself.
Invoking a Function with a Function Constructor
If a function invocation is preceded with the new
keyword, it is a constructor invocation.
It looks like you create a new function, but since JavaScript functions are objects you actually create a new object:
Example
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;
A constructor invocation creates a new object. The new object inherits the properties and methods from its constructor.
The this
keyword in the constructor does not have a value.
The value of this
will be the new object created when the function is invoked.
Practice Excercise Practice now
JavaScript Function Call
Method Reuse
With the call()
method, you can write a method that can be used on different objects.
All Functions are Methods
In JavaScript all functions are object methods.
If a function is not a method of a JavaScript object, it is a function of the global object (see previous chapter).
The example below creates an object with 3 properties, firstName, lastName, fullName.
Example
firstName:"John",
lastName: "Doe",
fullName: function () {
return this.firstName + " " + this.lastName;
}
}
// This will return "John Doe":
myObject.fullName();
The this Keyword
In a function definition, this
refers to the "owner" of the function.
In the example above, this
is the person object that "owns" the fullName function.
In other words, this.firstName means the firstName property of this object.
The JavaScript call() Method
The call()
method is a predefined JavaScript method.
It can be used to invoke (call) a method with an owner object as an argument (parameter).
With call()
, an object can use a method belonging to another object.
This example calls the fullName method of person, using it on person1:
Example
fullName: function() {
return this.firstName + " " + this.lastName;
}
}
const person1 = {
firstName:"John",
lastName: "Doe"
}
const person2 = {
firstName:"Mary",
lastName: "Doe"
}
// This will return "John Doe":
person.fullName.call(person1);
This example calls the fullName method of person, using it on person2:
Example
fullName: function() {
return this.firstName + " " + this.lastName;
}
}
const person1 = {
firstName:"John",
lastName: "Doe"
}
const person2 = {
firstName:"Mary",
lastName: "Doe"
}
// This will return "Mary Doe"
person.fullName.call(person2);
The call() Method with Arguments
The call()
method can accept arguments:
Example
fullName: function(city, country) {
return this.firstName + " " + this.lastName + "," + city + "," + country;
}
}
const person1 = {
firstName:"John",
lastName: "Doe"
}
person.fullName.call(person1, "Oslo", "Norway");
Practice Excercise Practice now
JavaScript Function Apply
Method Reuse
With the apply()
method, you can write a method that can be used on different objects.
The JavaScript apply() Method
The apply()
method is similar to the call()
method (previous chapter).
In this example the fullName method of person is applied on person1:
Example
fullName: function() {
return this.firstName + " " + this.lastName;
}
}
const person1 = {
firstName: "Mary",
lastName: "Doe"
}
// This will return "Mary Doe":
person.fullName.apply(person1);
The Difference Between call() and apply()
The difference is:
The call()
method takes arguments separately.
The apply()
method takes arguments as an array.
The apply() method is very handy if you want to use an array instead of an argument list.
The apply() Method with Arguments
The apply()
method accepts arguments in an array:
Example
fullName: function(city, country) {
return this.firstName + " " + this.lastName + "," + city + "," + country;
}
}
const person1 = {
firstName:"John",
lastName: "Doe"
}
person.fullName.apply(person1, ["Oslo", "Norway"]);
Compared with the call()
method:
Example
fullName: function(city, country) {
return this.firstName + " " + this.lastName + "," + city + "," + country;
}
}
const person1 = {
firstName:"John",
lastName: "Doe"
}
person.fullName.call(person1, "Oslo", "Norway");
Simulate a Max Method on Arrays
You can find the largest number (in a list of numbers) using the Math.max()
method:
Example
Since JavaScript arrays do not have a max() method, you can apply the Math.max()
method instead.
Example
The first argument (null) does not matter. It is not used in this example.
These examples will give the same result:
Example
Example
Example
JavaScript Strict Mode
In JavaScript strict mode, if the first argument of the apply()
method is not an object, it becomes the owner (object) of the invoked function. In "non-strict" mode, it becomes the global object.
Practice Excercise Practice now
Products
Partner
Copyright © RVR Innovations LLP 2024 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP