Skip to main content

Ban on "this"

@serverSerrverlesskiy

Removing the this keyword from JavaScript makes the language better!

This is because this depends on how the function was called, not where it was defined. Therefore, JavaScript's this is a source of much language confusion пут.

Using this ensures that the function works on the exact object in the context of which it was called. Through the this method, you can not only refer to any property of the object, but also pass a reference to the entire object itself somewhere (reducing the security of the application).

The value of this is called the calling context and will be determined when the function is called. For example, a function like this, declared without an object, is perfectly valid:

functionsay Hi() {
console.log(this.firstName)
}

This function does not yet know what this will be. This will be revealed when the program is executed.

If the same function is run in the context of different objects, it will receive a different this:

var user = { firstName: 'John' }
var admin = { firstName: 'Admin' }

function funcName() {
console.log(this.firstName)
}
user.f = funcName
admin.g = funcName

// this is equal to the object before point:
user.f() // Joni
admin.g() // Admin
admin['g']() // Admin (access to the object is implemented through square brackets)

So, the value of this does not depend on how the function was created, it is determined exclusively at the time of the call.

this and its disadvantages

Methods are functions that are stored in objects. In order for the function⚙️ to know which object to work on, use this.

Poor

But this loses context in many situations (return value unknown):

  • loses context inside nested functions
  • loses context in callbacks
  • this loses context when the method is used as an event handler.

Best language

The_best

JavaScript is both a functional programming language and a prototype-based language. If we get rid of this, we are left with JavaScript as a functional programming language. This is even better!

At the same time, without this JavaScript offers a new, unique way to do object-oriented programming without classes and inheritance.

Abandoning this

remember

The best way to avoid this related problems is not to use this at all!

JavaScript

JavaScript without this looks like the best functional programming language!

We can create encapsulated objects without using this as a collection of closures. With the help of React Hooks we can create stateful components without this.

The this keyword cannot be removed from JavaScript without destroying all existing applications. However, what can be done? We can write our own code without this and only allow it to be used in libraries. In the meantime, new rules ESLint are being introduced, prohibiting the use of this.

Since in the last lesson we abandoned classes, we say goodbye to this with them.

EnglishMoji!

Problems?

Problem

Write to Discord chat.

Questions:

Question

The this keyword is -

  1. The object to which the method belongs
  2. First argument in the function
  3. Set of properties

Is it possible to do without this:

  1. You can, and it is better not to use at all
  2. It is possible, but not advisable
  3. It is impossible, because this cannot be removed from JavaScript

JavaScript without this looks like the best:

  1. Functional programming language
  2. Procedural programming language
  3. Logical programming language

In order to understand how much you learned this lesson, take the test in the mobile application of our school on this topic or in our telegram bot.

EnglishMoji!

  1. Removing the 'this' keyword from JavaScript makes the language better
  2. The article "The this keyword in JavaScript"
  3. MDN web doc. Article "this"

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Dmitriy K.


Dmitriy Vasilev

💵

Resoner2005

🐛 🎨 🖋

Navernoss

🖋 🐛 🎨