This translation is incomplete. Please help translate this article from English.

ה"קי-וורד" this  מתנהג טיפה שונה בjs בהשוואה לשפות אחרות. יש לציין כי קיים הבדל בין strict mode ו- non-strict mode.

ברוב המקרים, הערך של this נקבע לפי הקריאה לפונקציה. לא ניתן לבצע השמה תוך הרצה, והערך עלול להיות שונה בכל קריאה לפונקציה. בES5 הוצג לראשונה bind לקביעת ערך לthis בפונקציה ללא קשר לקריאה, ובES2015 הוצג arrow functions.



Global Context

In the global execution context (outside of any function), this refers to the global object whether in strict mode or not.

// In web browsers, the window object is also the global object:

console.log(this === window); // true a = 37;

console.log(window.b) // "MDN"

console.log(window.a); // 37 this.b = "MDN";

console.log(b) // "MDN"

Function context

Inside a function, the value of this depends on how the function is called.

Simple call

Since the following code is not in strict mode, and because the value of this is not set by the call, this will default to the global object, which is window in a browser. 

function f1() {

return this;

// In a browser:


f1() === window; // true

// In Node:

f1() === global; // true

In strict mode, however, the value of this remains at whatever it was set to when entering the execution context, so, in the following case, this will default to undefined:

function f2() {
'use strict'; // see strict mode

return this; }

f2() === undefined; // true

So, in strict mode, if this was not defined by the execution context, it remains undefined.

In the second example, this should be undefined, because f2 was called directly and not as a method or property of an object (e.g. window.f2()). This feature wasn't implemented in some browsers when they first started to support strict mode. As a result, they incorrectly returned the window object.

To pass the value of this from one context to another, use call, or apply:

// An object can be passed as the first argument to call or apply and this will be bound to it.

var obj = {a: 'Custom'}; // This property is set on the global object var a = 'Global';

return this.a; // The value of this is dependent on how the function is called

function whatsThis(arg) { } whatsThis(); // 'Global'; // 'Custom'

whatsThis.apply(obj); // 'Custom'

Where a function uses the this keyword in its body, its value can be bound to a particular object in the call using the call or apply methods which all functions inherit from Function.prototype.

function add(c, d) {
return this.a + this.b + c + d;

} var o = {a: 1, b: 3};

// The first parameter is the object to use as

// 'this', subsequent parameters are passed as

// The first parameter is the object to use as

// arguments in the function call, 5, 7); // 16

// members are used as the arguments in the function call

// 'this', the second is an array whose

add.apply(o, [10, 20]); // 34

Note that with call and apply, if the value passed as this is not an object, an attempt will be made to convert it to an object using the internal ToObject operation. So if the value passed is a primitive like 7 or 'foo', it will be converted to an Object using the related constructor, so the primitive number 7 is converted to an object as if by new Number(7) and the string 'foo' to an object as if by new String('foo'), e.g.

function bar() {

}; // [object Number]'foo'); // [Object String]


The bind method

ECMAScript 5 introduced Function.prototype.bind. Calling f.bind(someObject) creates a new function with the same body and scope as f, but where this occurs in the original function, in the new function it is permanently bound to the first argument of bind, regardless of how the function is being used.

function f() {
  return this.a;


var g = f.bind({a: 'azerty'});

console.log(g()); // azerty

var h = g.bind({a: 'yoo'}); // bind only works once!

console.log(h()); // azerty var o = {a: 37, f: f, g: g, h: h};

console.log(o.f(), o.g(), o.h()); // 37, azerty, azerty

Arrow functions

In arrow functions, this retains the value of the enclosing lexical context's this. In global code, it will be set to the global object:

var globalObject = this;

var foo = (() => this);

console.log(foo() === globalObject); // true

Note: if this arg is passed to call, bind, or apply on invocation of an arrow function it will be ignored. You can still prepend arguments to the call, but the first argument (thisArg) should be set to null.

// Call as a method of an object

var obj = {func: foo};

console.log(obj.func() === globalObject); // true

// Attempt to set this using call

// Attempt to set this using bind

console.log( === globalObject); // true foo = foo.bind(obj);

console.log(foo() === globalObject); // true

No matter what, foo's this is set to what it was when it was created (in the example above, the global object). The same applies to arrow functions created inside other functions: their this remains that of the enclosing lexical context.

// Create obj with a method bar that returns a function that

// returns its this. The returned function is created as

// an arrow function, so its this is permanently bound to the

// this of its enclosing function. The value of bar can be set // in the call, which in turn sets the value of the

return x;

// returned function. var obj = {bar: function() { var x = (() => this); } };

// Call fn without setting this, would normally default

// Call bar as a method of obj, setting its this to obj // Assign a reference to the returned function to fn var fn =; // to the global object or undefined in strict mode

// Then calling the arrow function this is equals to window because it follows the this from bar.

console.log(fn() === obj); // true // But caution if you reference the method of obj without calling it var fn2 =;

console.log(fn2()() == window); // true

In the above, the function(call it anonymous function A) assigned to returns another function(call it anonymous function B) that is created as an arrow function. As a result, function B's  this is permanently set to the this of (function A)when called. When the returned function(function B) is called, its this will always be what it was set to initially. In the above code example, function B's this is set to function A's this which is obj, so it remains set to obj even when called in a manner that would normally set its this to undefined or the global object (or any other method as in the previous example in the global execution context).

As an object method

When a function is called as a method of an object, its this is set to the object the method is called on.

In the following example, when o.f() is invoked, inside the function this is bound to the o object.

var o = {
prop: 37,
f: function() {
return this.prop;

} };

console.log(o.f()); // 37

Note that this behavior is not at all affected by how or where the function was defined. In the previous example, we defined the function inline as the f member during the definition of o. However, we could have just as easily defined the function first and later attached it to o.f. Doing so results in the same behavior:

var o = {prop: 37};
function independent() {

return this.prop; }

console.log(o.f()); // 37

o.f = independent;


This demonstrates that it matters only that the function was invoked from the f member of o.

Similarly, the this binding is only affected by the most immediate member reference. In the following example, when we invoke the function, we call it as a method g of the object o.b. This time during execution, this inside the function will refer to o.b. The fact that the object is itself a member of o has no consequence; the most immediate reference is all that matters.

o.b = {g: independent, prop: 42};
console.log(o.b.g()); // 42

this on the object's prototype chain

The same notion holds true for methods defined somewhere on the object's prototype chain. If the method is on an object's prototype chain, this refers to the object the method was called on, as if the method were on the object.

var o = {f: function() { return this.a + this.b; }};

var p = Object.create(o); p.a = 1; p.b = 4;

console.log(p.f()); // 5

In this example, the object assigned to the variable p doesn't have its own f property, it inherits it from its prototype. But it doesn't matter that the lookup for f eventually finds a member with that name on o; the lookup began as a reference to p.f, so this inside the function takes the value of the object referred to as p. That is, since f is called as a method of p, its this refers to p. This is an interesting feature of JavaScript's prototype inheritance.

this with a getter or setter

Again, the same notion holds true when a function is invoked from a getter or a setter. A function used as getter or setter has its this bound to the object from which the property is being set or gotten.

function sum() {
return this.a + this.b + this.c;

} var o = { a: 1, b: 2,

return (this.a + this.b + this.c) / 3;

c: 3, get average() { } };

get: sum, enumerable: true, configurable: true});

Object.defineProperty(o, 'sum', {

console.log(o.average, o.sum); // 2, 6

As a constructor

When a function is used as a constructor (with the new keyword), its this is bound to the new object being constructed.

While the default for a constructor is to return the object referenced by this, it can instead return some other object (if the return value isn't an object, then the this object is returned).

* Constructors work like this:

* * function MyConstructor(){

* // Actual function body code goes here.

* // Create properties on |this| as

* // et cetera...

* // desired by assigning to them. E.g., * this.fum = "nom"; *

* // returns an object, that object will be the

* // If the function has a return statement that * // result of the |new| expression. Otherwise,

* // (i.e., the common case most usually seen).

* // the result of the expression is the object * // currently bound to |this| * } */ function C() { this.a = 37; } var o = new C();

console.log(o.a); // 38

console.log(o.a); // 37 function C2() { this.a = 37; return {a: 38}; }

o = new C2();

In the last example (C2), because an object was returned during construction, the new object that this was bound to simply gets discarded. (This essentially makes the statement "this.a = 37;" dead code. It's not exactly dead because it gets executed, but it can be eliminated with no outside effects.)

As a DOM event handler

When a function is used as an event handler, its this is set to the element the event fired from (some browsers do not follow this convention for listeners added dynamically with methods other than addEventListener).

// When called as a listener, turns the related element blue

function bluify(e) { // Always true

// true when currentTarget and target are the same object

console.log(this === e.currentTarget); console.log(this ===;

// Get a list of every element in the document = '#A5D9F3'; } var elements = document.getElementsByTagName('*');

for (var i = 0; i < elements.length; i++) {

// Add bluify as a click listener so when the // element is clicked on, it turns blue elements[i].addEventListener('click', bluify, false);


In an inline event handler

When the code is called from an inline on-event handler, its this is set to the DOM element on which the listener is placed:

<button onclick="alert(this.tagName.toLowerCase());">

Show this


The above alert shows button. Note however that only the outer code has its this set this way:

<button onclick="alert((function() { return this; })());">

Show inner this


In this case, the inner function's this isn't set so it returns the global/window object (i.e. the default object in non–strict mode where this isn't set by the call).


Specification Status Comment
ECMAScript Latest Draft (ECMA-262)
The definition of 'The this keyword' in that specification.
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'The this keyword' in that specification.
ECMAScript 5.1 (ECMA-262)
The definition of 'The this keyword' in that specification.
ECMAScript 3rd Edition (ECMA-262)
The definition of 'The this keyword' in that specification.
ECMAScript 1st Edition (ECMA-262)
The definition of 'The this keyword' in that specification.
Standard Initial definition. Implemented in JavaScript 1.0.

Browser compatibility

Update compatibility data on GitHub
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
thisChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes


Full support  
Full support

See also

Document Tags and Contributors

Contributors to this page: mdnwebdocs-bot, talforsher
Last updated by: mdnwebdocs-bot,