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.

Payment#

Now you are on a stripped-down version of the site, after subscribing to Patreon, you will get full access to the training course, as well as access to our server's private channels in Discord.

Download our mobile application or get tested in our JavaScript telegram bot, and also subscribe to our news.

Become a Patron!

Sumerian school

Contributors ✨#

Thanks goes to these wonderful people (emoji key):


Dmitriy K.

πŸ“–

Dmitriy Vasilev

πŸ’΅

Resoner2005

πŸ› 🎨 πŸ–‹

Navernoss

πŸ–‹ πŸ› 🎨

Become a Patron!