Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade For Teachers Spaces Get Certified Upgrade For Teachers Spaces
   ❮     
     ❯   

Basic JavaScript

JS TutorialJS SyntaxJS VariablesJS OperatorsJS If ConditionsJS LoopsJS StringsJS NumbersJS FunctionsJS ObjectsJS DatesJS ArraysJS Typed ArraysJS SetsJS MapsJS MathJS RegExpJS Data TypesJS ErrorsJS EventsJS ProgrammingJS ReferencesJS Versions

JS Advanced

JS FunctionsJS ObjectsJS ClassesJS IterationsJS AsynchronousJS ModulesJS Meta & ProxyJS HTML DOMJS WindowsJS Web APIJS AJAXJS JSONJS jQueryJS GraphicsJS ExamplesJS Reference


JavaScript Function Invocation


The code inside a JavaScriptfunction will execute when "something" invokes it.


Invoking a JavaScript Function

The code inside a function is not executed when the function isdefined.

The code inside a function is executed when the function isinvoked.

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 useinvoke, because a JavaScript function can be invoked without being called.


Invoking a Function as a Function

Example

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

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.

Note

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.

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

Example

function myFunction(a, b) {
  return a * b;
}
window.myFunction(10, 2);    // Will also return 20
Try it Yourself »

What isthis?

In JavaScript, thethis keyword refers to anobject.

Thethis keyword refers todifferent objects depending on how it is used:

Alone,this refers to theglobal object.
In a function,this refers to theglobal object.
In a function, in strict mode,this isundefined.
In an object method,this refers to theobject.
In an event,this refers to theelement that received the event.
Methods likecall(),apply(),andbind() can referthis toany object.

Note

this is not a variable.

this is a keyword.

You cannot change the value ofthis.



The Global Object

When a function is called without an owner object, the value ofthis becomes the global object.

In a web browser the global object is the browser window.

This example returns the window object as the value ofthis:

Example

let x = myFunction();           // x will be the window object

function myFunction() {
  return this;
}
Try it Yourself »

Invoking a function as a global function, causes the value ofthis to be the global object.
Using the window object as a variable can easily crash your program.


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 andlastName), and a method (fullName):

Example

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

ThefullName method is a function. The function belongs to the object.myObject is the owner of the function.

The thing calledthis, is the object that "owns" the JavaScript code. In this case the value ofthis ismyObject.

Test it! Change thefullName method to return the value ofthis:

Example

const myObject = {
  firstName:"John",
  lastName: "Doe",
  fullName: function () {
    return this;
  }
}

// This will return [object Object] (the owner object)
myObject.fullName();
Try it Yourself »

Invoking a function as an object method, causes the value ofthis to be the object itself.


Invoking a Function with the Constructor

If a function invocation is preceded with thenew 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

// 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;
Try it Yourself »

A constructor invocation creates a new object. The new object inherits the properties and methods from its constructor.

Thethis keyword in the constructor does not have a value.
The value ofthis will be the new object created when the function is invoked.



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.


[8]ページ先頭

©2009-2025 Movatter.jp