
PYTHON — JavaScript and Python- Understanding -this- and -self-
Technology, like art, is a soaring exercise of the human imagination. — Daniel Bell

PYTHON — Avoid Duplicate Animals in Python
JavaScript and Python: Understanding “this” and “self”
In this lesson, we will explore the differences between the this keyword in JavaScript and the self parameter in Python. The concept of the calling context of functions and the usage of this keyword in JavaScript will be covered. We will also compare it with the usage of self in Python. Let's dive in and understand the nuances of these concepts in both programming languages.
Understanding this in JavaScript
In JavaScript, the this keyword is used to refer to the context in which a function is called. Unlike Python, where self is explicitly passed to object methods, the value of this in JavaScript is determined by how the function is called.
# JavaScript Example
# The value of `this` is based on the calling function
# Global context
var x = this;
# Within an object method
var john = {
name: "John",
cleese: function() {
return this.name;
}
};In the above example, this is assigned to the global variable x, and within the cleese method of the john object, this refers to the containing object (john in this case).
Usage of this in Different Scenarios
When calling the cleese method using john.cleese(), this refers to the john object. However, when using a global-level function like print(), the context of this is the global context.
Arrow Functions and this
Arrow functions in JavaScript behave differently with respect to the this keyword. While they offer a cleaner syntax, mixing regular functions and arrow functions can lead to unexpected results. Arrow functions do not rebind the this value, and they can be particularly useful within object methods to maintain the context.
var fruit = {
items: ['apple', 'banana', 'pear'],
show: function() {
this.items.forEach((item) => {
console.log(this.name + ' likes ' + item);
});
}
};In the above example, the arrow function inside the forEach method retains the context of this as the fruit object, making it behave more like self in Python.
Handling this in JavaScript
JavaScript provides built-in functions such as .apply(), .call(), and .bind() to explicitly set the context for a calling function. These functions allow you to control the value of this within different contexts, providing flexibility in managing the calling context of functions.
Conclusion
Understanding the difference between the usage of this in JavaScript and self in Python is essential for developers working in both languages. While self is explicitly passed in Python, the value of this in JavaScript is based on the calling function, and arrow functions offer a way to maintain context similar to self in Python.
By grasping these distinctions, you can effectively manage the calling context of functions in both JavaScript and Python.
In summary, the differences in how this and self are handled in JavaScript and Python can impact how you write and structure your code in each language. It's important to understand these differences to avoid unexpected behavior and to utilize the right approach for managing the context of functions.







