Language: EN

que-es-this-en-programacion

What is this in programming

In object-oriented programming the concept of this in an instance method, is a reference to the own instance that owns the method.

This mechanism allows methods to access and modify the attributes of the instance, as well as invoke other methods defined in the same.

Depending on the programming language, a specific keyword (this, self, me, among others) is used to refer to this internal reference.

But, although they are implemented differently in different programming languages, they basically serve the same function.

On the other hand, by its own nature, this can only be used in instance methods. In static methods there is no instance, so it doesn’t make sense to use this.

Practical case

Let’s see an example of the use of this. In languages that incorporate the concept, this allows to refer to the current object within its instance methods.

Let’s assume the following example, with our Person class. In this case, we have an instance variable called name. But in turn, in a method (in this case in the constructor) we are receiving a variable name

class Person
{
    string name;

    Person(string name)
    {
        this.name = name; // 'this' refers to the 'name' field of the class
    }

    void ShowName()
    {
        Console.WriteLine("Name: " + this.name);
    }
}

In this case, by putting this allows us to disambiguate between the members of the class and the method parameters that have the same name.

In this example, this.name refers to the name field of the Person class, while name by itself refers to the constructor parameter.

What could be better than not using the same name? Well, yes, I’m not going to deny it 🤷‍♀️. But it’s just a simple example to illustrate the use of this.

Return this

In the context of a method, this is a reference to an object. Something special, but a reference. So it’s possible to return this. In fact, it’s not something strange.

class Person
{
    string Name;

    Person()
    {
        // ... we don't care in this example
    }

    Person DoSomething()
    {
        return this;  // can return itself
    }
}

In addition, this makes it possible to create certain mechanisms, such as the method chaining pattern or the builder pattern. We will see them in due time.

Examples in different languages

Finally, let’s see an example of how self-references work in different programming languages,

In C#, this is used to refer to the current instance of the class.

public class Person
{
    private string name;

    public Person(string name)
    {
        this.name = name;
    }

    public void Greet()
    {
        Console.WriteLine("Hello, my name is " + this.name);
    }
}

// Usage
Person person = new Person("John");
person.Greet();  // Output: Hello, my name is John

In C++, this is a pointer to the current instance of the class.

#include <iostream>
#include <string>

class Person {
private:
    std::string name;

public:
    Person(const std::string& name) : name(name) {}

    void greet() const {
        std::cout << "Hello, my name is " << this->name << std::endl;
    }
};

// Usage
int main() {
    Person person("John");
    person.greet();  // Output: Hello, my name is John
    return 0;
}

In TypeScript, this is used within methods to access the attributes and other methods of the instance.

class Person {
    private name: string;

    constructor(name: string) {
        this.name = name;
    }

    greet(): void {
        console.log("Hello, my name is " + this.name);
    }
}

// Usage
const person = new Person("John");
person.greet();  // Output: Hello, my name is John

In JavaScript, this is used in a similar way to TypeScript to refer to the current instance.

class Person {
    constructor(name) {
        this.name = name;
    }

    greet() {
        console.log("Hello, my name is " + this.name);
    }
}

// Usage
const person = new Person("John");
person.greet();  // Output: Hello, my name is John

In Python, self is used as the first parameter of instance methods to refer to the current instance of the object.

class Person:
    def __init__(self, name):
        self.name = name

    def greet(self):
        print(f"Hello, my name is {self.name}")

# Usage
person = Person("John")
person.greet()  # Output: Hello, my name is John