JavaScript From Beginner To Advanced

0% completed

Previous
Next
JavaScript - this Keyword

The this keyword is a fundamental concept in JavaScript, providing a way to refer to the context in which the current code is executing. Understanding how this behaves across different contexts is crucial for mastering JavaScript, especially when working with object-oriented programming and handling events.

In JavaScript, the value of this is determined by how a function is called. Its value cannot be set by assignment during execution, and it may be different each time the function is called.

Core Contexts of this

  1. Global Context: In the global execution context (outside of any function), this refers to the global object whether in strict mode or not. In a browser, this refers to the window object.
  2. Function Context:
    • Regular Functions: In simple function calls, this is bound to the global object, or undefined in strict mode.
    • Method Calls: When a function is called as a method of an object, this is bound to the object the method is called on.
  3. Constructor Context: In a constructor function, this is bound to the newly created object instance.
  4. Arrow Functions: Do not have their own this context; instead, this is lexically inherited from the outer function where the arrow function is defined.

Example 1: this in a Method

Javascript
Javascript

. . . .

Explanation:

  • Object Definition (const person = {...}):
    • Declares an object person with properties firstName, lastName, and a method fullName.
  • Properties (firstName, lastName):
    • These are straightforward properties storing strings.
  • Method (fullName):
    • A method defined using the function keyword, which allows this to correctly refer to the person object when the method is called.
    • Inside fullName, this.firstName accesses the firstName property of the person object. Similarly, this.lastName accesses lastName.
  • Method Invocation (person.fullName()):
    • When fullName is called, this inside fullName refers to person, enabling access to the firstName and lastName properties. The method returns the concatenated string of these properties.
  • Output Statement (console.log(...)):
    • Logs the result of the fullName() method to the console, displaying "John Doe".

Example 2: this with Arrow Functions

Javascript
Javascript

. . . .

Explanation:

  • Object Definition (const person = {...}):
    • Similar to the first example, but fullName is now defined using an arrow function.
  • Arrow Function (fullName):
    • fullName is an arrow function, which does not bind its own this. Therefore, this inside fullName does not refer to the person object but inherits this from its enclosing execution context (typically global or window in a browser).
  • Attempt to Access Properties (this.firstName, this.lastName):
    • Since this is not bound to person, this.firstName and this.lastName do not point to the properties of person but to the global context, where these properties are likely undefined.
  • Method Invocation and Output (console.log(...)):
    • Outputs the result of the fullName() method. In a typical browser context without corresponding global variables, this will print undefined undefined.

These examples underscore the importance of understanding the scope and binding of this in JavaScript, particularly when choosing between function expressions and arrow functions for methods within objects.

Why Use this?

  • Contextual Binding: this allows functions within objects to access other properties of the same object, acting as a reference to the object itself.
  • Reusability: It makes function methods more flexible and reusable across different objects.
  • Control Over Scope: In event handlers and methods, this provides a direct way to access the object that owns the current executing method.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next