cuando usar this or that

When to Use This or That: A Comprehensive Guide

By Emily Miller
AD

When it comes to making decisions, we often face the dilemma of choosing between two or more options. In the digital world, this is no different. Two popular programming constructs that often leave developers scratching their heads are "this" and "that". Both serve different purposes and it's crucial to understand when to use each to ensure efficient and effective code. Let's delve into the world of "this" and "that" to understand when to use each.

Before we dive into the details, let's clarify that in programming, "this" and "that" are not keywords but rather references to objects. They are used to access members of an object, but they behave differently based on the context. Now, let's explore when to use each.

Using "this"

"this" is a reference variable that holds the current object—the object whose method or constructor is called. It's used to access the members of the current object. So, when should you use "this"?

AD
This, That, These y Those
This, That, These y Those
THIS or THAT
THIS or THAT
Uses of This or That worksheet for grade 2
Uses of This or That worksheet for grade 2

Firstly, use "this" to refer to the instance variables of the current object. For example, consider the following code snippet:

class Dog {
  constructor(name) {
    this.name = name;
  }
  bark() {
    console.log(`${this.name} says: Woof!`);
  }
}

In this case, "this" is used to refer to the instance variable "name".

Схема this that these those
Схема this that these those
an advertisement with different types of cell phones
an advertisement with different types of cell phones
This or that?
This or that?

Using "this" in methods

Secondly, use "this" to call other methods of the current object. This can be useful when you want to call a method from within another method of the same class. Here's an example:

class Cat {
  constructor(name) {
    this.name = name;
  }
  meow() {
    console.log(`${this.name} says: Meow!`);
  }
  purr() {
    this.meow(); // Calling the meow method using "this"
  }
}

In this case, "this" is used to call the "meow" method from within the "purr" method.

AD

Using "this" in constructors

Lastly, use "this" in constructors to initialize instance variables. This helps avoid naming collisions between instance variables and function parameters. Here's an example:

a poster with clothes and shoes on it that says, this or that order?
a poster with clothes and shoes on it that says, this or that order?
THIS OR THAT?????!!! Makeup To Get From Sephora, Makeup To Get At Sephora, What Should I Get From Sephora, Sephora Favorites Vacay All Day, Would You Rather Makeup Edition, This Or That Celebrity Edition, Would You Rather Skincare Edition, Pick Your Makeup Routine, Good Makeup From Sephora
THIS OR THAT?????!!! Makeup To Get From Sephora, Makeup To Get At Sephora, What Should I Get From Sephora, Sephora Favorites Vacay All Day, Would You Rather Makeup Edition, This Or That Celebrity Edition, Would You Rather Skincare Edition, Pick Your Makeup Routine, Good Makeup From Sephora
this or that This Or That Clothes
this or that This Or That Clothes
an advertisement with the words, this or that
an advertisement with the words, this or that
#This or That Aesthetic This Or That, Fall Edition This Or That Game, This Or That Questions Fall Edition, What Are Some Fall Emojis, Fall This Or That, This Or That Questions Fall, September This Or That, This Or That Autumn Edition, This Or That Fall Edition
#This or That Aesthetic This Or That, Fall Edition This Or That Game, This Or That Questions Fall Edition, What Are Some Fall Emojis, Fall This Or That, This Or That Questions Fall, September This Or That, This Or That Autumn Edition, This Or That Fall Edition
-🎀THISORTHAT🎀-
-🎀THISORTHAT🎀-
class Bird {
  constructor(name, age) {
    this.name = name; // Using "this" to initialize the instance variable "name"
    this.age = age; // Using "this" to initialize the instance variable "age"
  }
}

In this case, "this" is used to initialize the instance variables "name" and "age".

Using "that"

"that" is not a keyword in JavaScript, but it's often used in a similar context as "this". So, when should you use "that"?

AD

Firstly, use "that" to refer to an object that is returned from a function. This can be useful when you want to use the returned object immediately after it's created. Here's an example:

A fun game if this or that
A fun game if this or that
a menu with different drinks on it
a menu with different drinks on it
a poster with different words and pictures on the back ground, including an image of a cat
a poster with different words and pictures on the back ground, including an image of a cat
there are many different items that can be seen in this image, including an iphone case and
there are many different items that can be seen in this image, including an iphone case and
this is an image of a tv screen with the words'this or that '
this is an image of a tv screen with the words'this or that '
this or that pets word list
this or that pets word list
This or That ??? | credit to me if you repost
This or That ??? | credit to me if you repost
Free Printable Easter This or That Game
Free Printable Easter This or That Game
...OR...
...OR...
This Or That
This Or That
This That These Those, Using and Differences - English Grammar Here
This That These Those, Using and Differences - English Grammar Here
this or that animals is listed in the animal names list for each type of animal
this or that animals is listed in the animal names list for each type of animal
a poster with the words, this or that? and different types of objects on it
a poster with the words, this or that? and different types of objects on it
a poster with different types of words on it
a poster with different types of words on it
function createPerson(name, age) {
  const person = {
    name,
    age,
    greet() {
      console.log(`Hello, I'm ${this.name} and I'm ${this.age} years old.`);
    }
  };
  return person;
}

const john = createPerson('John Doe', 30);
john.greet(); // Hello, I'm John Doe and I'm 30 years old.

In this case, "that" is used to refer to the object that is returned from the "createPerson" function.

Using "that" with closures

Secondly, use "that" with closures to preserve the correct "this" value. In JavaScript, the "this" value is determined by how a function is called, not where it's defined. This can lead to unexpected behavior with closures. Here's an example:

AD
const button = document.getElementById('myButton');
button.addEventListener('click', function() {
  setTimeout(function() {
    console.log(this); // This will log the global object (window), not the button
  }, 1000);
});

In this case, "that" can be used to preserve the correct "this" value:

const button = document.getElementById('myButton');
button.addEventListener('click', function() {
  const that = this; // Using "that" to preserve the correct "this" value
  setTimeout(function() {
    console.log(that); // This will log the button
  }, 1000);
});

In this case, "that" is used to preserve the correct "this" value within the closure.

In conclusion, while "this" and "that" are not keywords in JavaScript, they serve important roles in referencing objects. Understanding when to use each can help you write more efficient and effective code. So, the next time you're wondering when to use "this" or "that", remember the guidelines outlined above. Happy coding!

AD