DEV Community

Discussion on: Rethinking JavaScript: The complete elimination and eradication of JavaScript's this.

Collapse
 
sampsonprojects profile image
Sampson Crowley

If you're having so much of an issue with this, you don't understand JavaScript. Your scope can't "change when you don't expect it to" as you claim in a comment thread. YOU just aren't paying attention. I've literally never once had an issue with proper bindings. I bind or apply when needed, or use an arrow function when I need to keep a scope

Collapse
 
joelnet profile image
JavaScript Joel

You seem to be suffering from the Dunning–Kruger effect. Just because you do not have a problem does not mean that others do not. A simple search on stack overflow or google will show this is a problem for many JavaScript developers.

Take a look at this code:

const logSpeak = animal => console.log(animal.speak())
//=> "meow"
const logSpeak = ({ speak }) => console.log(speak())
// undefined

By simply converting a function to use argument destructuring, you will cause the function to fail. It is not reasonable to assume this.

When you bind or apply or use arrow functions to use the parent scope, these are all things that had to be learned. Not everyone is at that level. Do you remember WHY you had to learn those things?

I do not know a single JavaScript developer that hasn't written console.log(this) to figure out WTF this is. And if you tell me you haven't, I'll call you a liar ;)

Cheers!

Collapse
 
sampsonprojects profile image
Sampson Crowley • Edited

this is not surprising that it changes during destruction if you actually understand the language.

Objects have their own this. Do you understand how destructuring works? It's absolutely reasonable to expect this to change

Thread Thread
 
joelnet profile image
JavaScript Joel

Absolutely! There are very valid reasons why this is happening. And it goes all the way back to the early versions of JavaScript, back in the Netscape days before classes were introduced. I remember this because I have been programming in JavaScript for over 20 years now.

The ability to bind a this to a function made total sense back then. But then people tried doing OOP in JavaScript. People would attempt to make classes with Inheritance. JavaScript wasn't designed for that.

Now we have an actual class, which I believe was a mistake to introduce to the language. Because when people see class, they do not understand how it works under the hood. Class is just syntactic sugar after all. They incorrectly assume it will function in a similar way to how a class works in other languages.

So sure, after you understand everything surrounding the concept of this, you will run into fewer bugs with it. But you are still going to run into bugs. I do not know a single developer that hasn't written console.log(this) to figure out what this is. "Oh it's window. Duh!"

NULL is commonly referred to as the Billion dollar mistake. How much do you think this is going to cost us?