WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JavaScript Function Invocation


JavaScript capacities can be conjured in 4 diverse ways.

Each technique contrasts in how this is instated.


The this Keyword

In JavaScript, the thing called this, is the article that "owns" the current code.

The estimation of this, when utilized in a capacity, is the article that "owns" the capacity.


Invoking a JavaScript Function

You have effectively discovered that the code inside a JavaScript capacity will execute when "something" conjures it.

The code in a capacity isn't executed when the capacity is defined. It is executed when the capacity is invoked.

Some individuals utilize the term "call a function" rather than "invoke a function".

It is likewise very regular to state "call upon a function", "start a function", or "execute a function".

In this instructional exercise, we will utilize invoke, in light of the fact that a JavaScript capacity can be summoned without being called.


Invoking a Function as a Function

Example

work myFunction(a, b) {
    return a * b;
}
myFunction(10, 2);          /myFunction(10, 2) will return 20
Try it Yourself »

The work above does not have a place with any article. Be that as it may, in JavaScript there is dependably a default worldwide object.

In HTML the default worldwide article is simply the HTML page, so the capacity above "belongs" to the HTML page.

In a program the page object is the program window. The capacity above consequently turns into a window function.

myFunction() and window.myFunction() is the equivalent function:

Example

work myFunction(a, b) {
    return a * b;
}
window.myFunction(10, 2);    //window.myFunction(10, 2) will likewise return 20
Try it Yourself »


The Global Object

When a capacity is called without a proprietor object, the estimation of this turns into the worldwide object.

In an internet browser the worldwide item is the program window.

This precedent returns the window object as the estimation of this:

Example

work myFunction() {
    return this;
}
myFunction();               /Will restore the window object
Try it Yourself »

Invoking a Function as a Method

In JavaScript you can characterize work as article methods.

The following precedent makes an article (myObject), with two properties (firstName and lastName), and a technique (fullName):

Example

var myObject = {
    firstName:"John",
    lastName: "Doe",
    fullName: work () {
        return this.firstName + " " + this.lastName;
    }
}
myObject.fullName();        /Will return "John Doe"
Try it Yourself »

The fullName strategy is a capacity. The capacity has a place with the item. myObject is the proprietor of the function.

The thing called this, is the item that "owns" the JavaScript code. For this situation the estimation of this is myObject.

Test it! Change the fullName strategy to restore the estimation of this:

Example

var myObject = {
    firstName:"John",
    lastName: "Doe",
    fullName: work () {
        return this;
    }
}
myObject.fullName();         /Will return [object Object] (the proprietor object)
Try it Yourself »

JavaScript capacities can be conjured in 4 diverse ways.

Each technique contrasts in how this is instated.


The this Keyword

In JavaScript, the thing called this, is the article that "owns" the current code.

The estimation of this, when utilized in a capacity, is the article that "owns" the capacity.


Invoking a JavaScript Function

You have effectively discovered that the code inside a JavaScript capacity will execute when "something" conjures it.

The code in a capacity isn't executed when the capacity is defined. It is executed when the capacity is invoked.

Some individuals utilize the term "call a function" rather than "invoke a function".

It is likewise very regular to state "call upon a function", "start a function", or "execute a function".

In this instructional exercise, we will utilize invoke, in light of the fact that a JavaScript capacity can be summoned without being called.


Invoking a Function as a Function

Example

work myFunction(a, b) {
    return a * b;
}
myFunction(10, 2);          /myFunction(10, 2) will return 20
Try it Yourself »

The work above does not have a place with any article. Be that as it may, in JavaScript there is dependably a default worldwide object.

In HTML the default worldwide article is simply the HTML page, so the capacity above "belongs" to the HTML page.

In a program the page object is the program window. The capacity above consequently turns into a window function.

myFunction() and window.myFunction() is the equivalent function:

Example

work myFunction(a, b) {
    return a * b;
}
window.myFunction(10, 2);    //window.myFunction(10, 2) will likewise return 20
Try it Yourself »


The Global Object

When a capacity is called without a proprietor object, the estimation of this turns into the worldwide object.

In an internet browser the worldwide item is the program window.

This precedent returns the window object as the estimation of this:

Example

work myFunction() {
    return this;
}
myFunction();               /Will restore the window object
Try it Yourself »

Invoking a Function as a Method

In JavaScript you can characterize work as article methods.

The following precedent makes an article (myObject), with two properties (firstName and lastName), and a technique (fullName):

Example

var myObject = {
    firstName:"John",
    lastName: "Doe",
    fullName: work () {
        return this.firstName + " " + this.lastName;
    }
}
myObject.fullName();        /Will return "John Doe"
Try it Yourself »

The fullName strategy is a capacity. The capacity has a place with the item. myObject is the proprietor of the function.

The thing called this, is the item that "owns" the JavaScript code. For this situation the estimation of this is myObject.

Test it! Change the fullName strategy to restore the estimation of this:

Example

var myObject = {
    firstName:"John",
    lastName: "Doe",
    fullName: work () {
        return this;
    }
}
myObject.fullName();         /Will return [object Object] (the proprietor object)
Try it Yourself »