A comparison of ‘this’ in JavaScript and that ‘self’ in Python

Bo Louie
5 min readDec 19, 2022

--

Photo by Di Bella Coffee

In object-oriented programming, this and self are both used to refer to the current instance of the object that a method is being called on.

An object is a data structure that contains data fields and methods. An object represents a specific instance of a class, which is a template or blueprint for creating objects.

A method is a function that is associated with an object and is used to perform some action or computation on the object. Methods are defined in a class definition and can be called on an object to perform a specific task.

In JavaScript, the this keyword refers to the current object that is executing the current function. It is a way to access the properties and methods of an object from within the object itself. The value of this is determined by how a function is called.

Here is an example of how this is used in JavaScript:

const person = {
name: 'John',
sayHi: function() {
console.log(`Hi, my name is ${this.name}`);
}
}

person.sayHi(); // Output: "Hi, my name is John"

In most object-oriented programming languages, when you define a class, you can define methods that can be called on instances of that class. When you call a method on an instance of a class, the instance is passed to the method as an implicit parameter.

In Python, the self parameter is used to refer to the instance of a class. It is equivalent to this in other object-oriented languages. The self parameter is used to access the attributes and methods of a class from within the class itself.

And here is an example of how self is used in Python:

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

def say_hi(self):
print(f"Hi, my name is {self.name}")

person = Person("John")
person.say_hi() # Output: "Hi, my name is John"

Debugging Python

One common bug that can occur when using the self parameter in Python is forgetting to include it as the first parameter in a method definition. This can cause a TypeError to be raised when the method is called, because the method is expecting the self parameter to be passed in.

Here is an example of this type of bug:

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

def say_hi(): // missing self parameter
print(f"Hi, my name is {self.name}")

person = Person("John")
person.say_hi()

In this example, we have defined a Person class with a say_hi method that is supposed to print a greeting using the name attribute of the Person instance. However, we have forgotten to include the self parameter in the method definition, so when we try to call the say_hi method on the person instance, a TypeError is raised.

To fix this bug, we need to include the self parameter in the method definition like this:

def say_hi(self):
print(f"Hi, my name is {self.name}")

Now the say_hi method will work as expected and will print the greeting using the name attribute of the Person instance.

Debugging JavaScript

One common bug that can occur when using the this keyword in JavaScript is using this inside a function that is not bound to an object. In this case, the value of this will be set to the global object, which can lead to unexpected behavior.

Here is an example of this type of bug:

const person = {
name: 'John',
sayHi: function() {
console.log(`Hi, my name is ${this.name}`);
}
}

const greet = person.sayHi;
greet(); // Output: "Hi, my name is undefined"

In this example, we have an object person with a sayHi method that logs a greeting using the name property of the object. We then create a new variable greet and assign it the value of the sayHi method.

When we call the greet function, the value of this inside the function is set to the global object, which does not have a name property. As a result, the greeting logs undefined instead of the expected value.

To fix this bug, we can either bind the value of this to the person object when creating the greet variable, or we can use an arrow function for the sayHi method, which does not have its own this value and uses the this value of the surrounding context.

Here is how we could fix the example using the bind method:

const person = {
name: 'John',
sayHi: function() {
console.log(`Hi, my name is ${this.name}`);
}
}

const greet = person.sayHi.bind(person);
greet(); // Output: "Hi, my name is John"

And here is how we could fix the example using an arrow function:

const person = {
name: 'John',
sayHi: () => {
console.log(`Hi, my name is ${this.name}`);
}
}

const greet = person.sayHi;
greet(); // Output: "Hi, my name is John"

It is important to understand the this variable in JavaScript and the self parameter in Python because they are essential for working with objects and classes in these languages.

In JavaScript, the this keyword is used to access the properties and methods of an object from within the object itself. Understanding how the value of this is determined and how to use it correctly is essential for working with objects in JavaScript.

In Python, the self parameter is used to refer to the instance of a class and access its attributes and methods. Understanding how to use the self parameter correctly is essential for working with classes and object-oriented programming in Python.

Without a proper understanding of this in JavaScript and self in Python, it is difficult to write correct and maintainable code that uses objects and classes.

Read my other post ‘A few things about JavaScript and Python for more similarities and differences between the two programming languages.

Disclaimer: This article was created with the help of AI.

Thanks for reading and hopefully you found this helpful. I’m open to feedback and if you find an error, please kindly let me know about it.

References:

Amos, D. et al. (2021). Python Basics: A practical introduction to python 3. Fourth Edition.

Anthony Alicea’s Udemy Course: JavaScript Understanding the Weird Parts

https://openai.com/blog/chatgpt/

Haverbeke, M. (2018). Eloquent JavaScript 3rd Ed.: A modern introduction to programming. No Starch Press.

--

--

Bo Louie

transformer | doing design research and web development things.