DEV Community

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

 
joelnet profile image
JavaScript Joel • Edited

That is because you have been taught to use this. With OOP, you are pretty much stuck with this (unless of course you use nothis). When you learn functional reactive programming, you'll find this to no longer be necessary and magically this just vanishes from your codebase.

Check out one of my projects here github.com/joelnet/bitcoin-all-tim.... This is a real world application. It follows what I preach from How I rediscovered my love for JavaScript after throwing 90% of it in the trash.

You won't find any references to this, var, let, if, switch, for, the function keyword or other staples of classic javascript programs.

Thread Thread
 
ilmtitan profile image
Jim Przybylinski • Edited

If you use typescript and modify the cat speak code to use an explicit if, you get a compile error.

const cat = {
    sound: 'meow',
    speak(this: {sound: string}) { return this.sound }
}

const speak = cat.speak
speak() // <= Compile error: void is not assignable to { sound: string }.
Thread Thread
 
joelnet profile image
JavaScript Joel

It's pretty nice that this will happen at compile time and not runtime.