DEV Community

Asma (Behnaz) Sormeily
Asma (Behnaz) Sormeily

Posted on

Navigating the Intricacies of "This" and "Self" in Javascript and Python

Introduction

In this article, we'll delve into the inheritance models of Javascript and Python, focusing on the "this" keyword in Javascript and the "self" reference in Python. We often find these concepts perplexing, especially when transitioning between these languages. Let's demystify them by examining their similarities and differences.

Understanding Constructor Functions

Before we dive into inheritance, it's crucial to understand constructor functions in both languages. These functions are used to create instances of classes, setting up initial properties and behaviors.

Inheritance Simplified

Inheritance, a cornerstone of object-oriented programming (OOP), allows us to create subclasses from a superclass. These subclasses inherit properties and methods but can also extend or modify them to suit their specific requirements.

Python Inheritance Model

Python's inheritance model is structured and traditional, resembling other class-based languages like Java and C++. It employs classes and constructors for inheritance. Key patterns include:

  • Single Inheritance: Inherits from one parent class.
  • Multiple Inheritance: Inherits from more than one parent class.
  • Super Function: Python's super() function, akin to JavaScript's super, calls methods from the parent class.

Example:

class Cat:
    def __init__(self, color):
        self.color = color

class HouseCat(Cat):
    def __init__(self, color, name):
        super().__init__(color)
        self.name = name
Enter fullscreen mode Exit fullscreen mode

JavaScript Inheritance Model

JavaScript uses a prototypal inheritance model. In a prototype-based language like JavaScript, if a property isn't found in an object, the JavaScript engine searches the object's prototype chain.

  • "This" in JavaScript: Refers to the current object instance and is a reserved keyword. It's context-dependent and can represent different values based on how it's used.

Example:

function Animal(name) {
    this.name = name;
}

Animal.prototype.speak = function () {
    console.log(`${this.name} makes a noise.`);
}
Enter fullscreen mode Exit fullscreen mode
  • "Self" in Python: In Python, self represents the instance of the class and is explicitly passed to each instance method. It's not a reserved keyword but a strong convention in Python OOP.

Example:

class Employee:
    def __init__(self, firstname, lastname, salary):
        self.firstname = firstname
        self.lastname = lastname
        self.salary = salary

class Developer(Employee):
    def __init__(self, firstname, lastname, salary, stack, experience):
        super().__init__(firstname, lastname, salary)
        self.stack = stack
        self.experience = experience
Enter fullscreen mode Exit fullscreen mode

Conclusion

Understanding "this" in JavaScript and "self" in Python is pivotal for mastering OOP in these languages. Despite their conceptual similarity as references to the current instance, their usage and the inheritance models of their respective languages present unique features worth comprehending for any developer.

Top comments (0)