Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript this keyword

JavaScript this keyword

In object-oriented languages, this represents a reference to the current object.

But in JavaScript, this is not fixed; it changes with the execution context.

Example

var

person

=

{

firstName

:

"

John

"

,

lastName

:

"

Doe

"

,

id

:

5566

,

fullName

:

function

(

)

{

return

this

.

firstName

"

"

this

.

lastName

;

}

}

;

Try it »


this in a method

In an object method, this refers to the object that called the method.

In the example above, this refers to the person object.

The object that owns the fullName method is person.

Example

fullName

:

function

(

)

{

return

this

.

firstName

"

"

this

.

lastName

;

}

Try it »


Using this alone

When used alone, this refers to the global (Global) object.

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

Example

var

x

=

this

;

Try it »

In strict mode, if used alone, this also refers to the global (Global) object.

Example

"

use strict

"

;

var

x

=

this

;

Try it »


Using this in a function (default)

In a function, the function's owner is bound to this by default.

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

Example

function

myFunction

(

)

{

return

this

;

}

Try it »


Using this in a function (strict mode)

In strict mode, a function is not bound to this, and this is undefined。

Example

"

use strict

"

;

function

myFunction

(

)

{

return

this

;

}

Try it »


this in an event

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

Example

<button onclick="this.style.display='none'">

Click me and I will disappear

button

Try it »


Binding in object methods

In the example below, this is the person object, which is the owner of the function:

Example

var

person

=

{

firstName

:

"

John

"

,

lastName

:

"

Doe

"

,

id

:

5566

,

myFunction

:

function

(

)

{

return

this

;

}

}

;

Try it »

Example

var

person

=

{

firstName

:

"

John

"

,

lastName

:

"

Doe

"

,

id

:

5566

,

fullName

:

function

(

)

{

return

this

.

firstName

"

"

this

.

lastName

;

}

}

;

Try it »

Note: this.firstName indicates this (person) object's firstName attribute.


Explicit function binding

In JavaScript, functions are also objects, and objects have methods; apply and call are methods of function objects. These two methods are extremely powerful: they allow you to switch the context in which a function executes, that is, the object to which this is bound.

In the example below, when we call the person1.fullName method with person2 as an argument, this will refer to person2, even though it is a method of person1:

Example

var

person1

=

{

fullName

:

function

(

)

{

return

this

.

firstName

"

"

this

.

lastName

;

}

}

var

person2

=

{

firstName

:

"

John

"

,

lastName

:

"

Doe

"

,

}

person1

.

fullName

.

call

(

person2

)

;

//

returns "John Doe"

Try it »