JS: “this” Binding

By Xah Lee. Date: . Last updated: .

Purpose of “this” Binding

In JavaScript, when a function is called, it has a associated value called “this binding”. (Except arrow functions. [see JS: Arrow Function])

In function body, the “this binding” is represented by the keyword this.

The purpose of “this binding” is to allow function to implicitly work with a object. (without actually passing the object as argument.)

For example, when you call myArray.push(), the function push's “this binding” is myArray. That's how push knows about myArray.

A function may or may not make use of “this binding”. For example, if a user defined function body does not contain this keyword, then the associated “this binding” does nothing.

What's the Value of “this”?

The “this binding” is designed for use with function only. Typically, for function call of the form obj.method_name(), where the function method_name's “this binding” will be obj.

When this is evaluated outsite a function, its value is the global object. In browser, it is the window object. [see JS: Browser Window Object, DOM]

When this is used inside a function and the function is called, the value of this depends on how the function is called. Here's a summary:

Following are details.

“this” in Method Call

The most common use is calling a function as a method of some object. For example, myobj.f(). In such case, the value of this is the object that the method f is a property of. (the myobj)

let ob = { k1: function () { return this; }};
console.log(ob === ob.k1()); // true

“this” in Constructor Call (new f())

When a function is invoked as a constructor (for example, new F() or new obj.F()), its this value is the newly created object. (similar to this = Object.create(F.prototype))

[see JS: Operator “new”]

// typical example of using 「this」

function Ff (x) { this.k1 = x; }

console.log ( new Ff(4) );
// Ff { k1: 4 }

“this” in Global Function Call

When a function is called in a global context and not as a method of another object, the this evaluates to undefined if strict mode is on, else it's the global object.

"use strict"

function Ff () { { return this; } }

console.log( Ff() === undefined); // true
// in non-strict mode, in browser, 「this」 is the global object 「window」

function F2 () { { return this; } }

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

[see JS: What's Strict Mode and How to Determine Strict Mode?]

“this” in Nested Functions

In a nested function (for example, g is nested inside f), when g is called inside f, the value of g's this is undefined if strict mode is on, else the global object.

// 「this」 is in nested function has value undefined, if under use strict

"use strict"

function ff () {
    const gg = function () {
        return this;
    };
    return gg();
};

console.log( ff() === undefined); // true

// if in non strict mode, it returns the global object.

Call Function with any thisBinding

You can call a function with its this set to any object you want. You can do this by using the method “call” or “apply”. See: JS: Function Call, Apply, Bind.

JS Constructor/Class

  1. “this” Binding
  2. What's Constructor?
  3. Property Key "prototype"
  4. Operator “new”
  5. Operator “instanceof”
  6. Property Key “constructor”
  7. Class
  8. Keyword “extends”
  9. Keyword “super”
Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. CSS
  3. JavaScript
  4. JS Obj Ref
  5. DOM
  6. SVG
  7. Blog