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


JavaScriptthis in Functions


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.


this Alone

When used alone,this refers to theglobal object.

Becausethis is in the global scope.

In a browser window the global object is[object Window]:

Example

let x = this;
Try it Yourself »

 Instrict mode, when used alone,this also refers to theglobal object:

Example

"use strict";
let x = this;
Try it Yourself »

this in a Function (Default)

In a function, by default,this is theglobal object.

In a browser window, the global object is[object Window]:

Example

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


this in a Function (Strict)

JavaScriptstrict mode does not allow default binding.

When used in a function, in strict mode,this isundefined.

Example

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

this in Event Handlers

In HTML event handlers,this refers to the HTML element that received the event:

Example

<button onclick="this.style.display='none'">
  Click to Remove Me!
</button>

Try it Yourself »


this in Arrow Functions

In regular functions thethis keyword represented the object that called the function, which could be the window, the document, a button or whatever.

With arrow functions thethis keywordalways represents the object that defined the arrow function.

In short, with arrow functions there are no binding ofthis.

Note

Arrow functions do not have their ownthis.

They are not well suited for definingobject methods.

Let us take a look at two examples to understand the difference.

Both examples call a method twice, first when the page loads, and once again when the user clicks a button.

The first example uses a regular function, and the second example uses an arrow function.

The result shows that the first example returns two different objects (window and button), and the second example returns the window object twice, because the window object is the "owner" of the function.

Example

With a regular functionthis represents the object thatcalls the function:

// Regular Function:
hello = function() {
  document.getElementById("demo").innerHTML += this;
}

// The window object calls the function:
window.addEventListener("load", hello);

// A button object calls the function:
document.getElementById("btn").addEventListener("click", hello);

Try it Yourself »

Example

With an arrow functionthis represents theowner of the function:

// Arrow Function:
hello = () => {
  document.getElementById("demo").innerHTML += this;
}

// The window object calls the function:
window.addEventListener("load", hello);

// A button object calls the function:
document.getElementById("btn").addEventListener("click", hello);

Try it Yourself »

Remember these differences when you are working with functions. Sometimes the behavior of regular functions is what you want, if not, use arrow functions.


this Precedence

Use the following precedence of order to determine which objectthis refers to:

OrderObjectBecause
1bind()this is in a function being called using bind()
2apply()this is in a function being called using apply()
2call()this is in a function being called using call()
3Object methodthis is in an object function (method)
4Global scopethis is in a function in the global scope



×

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