DEV Community

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

Collapse
 
dechamp profile image
DeChamp • Edited

Doing away with certain parts of a language and replacing it with an excessive amount of additional code for the sake of "it's too hard" doesn't make sense.

Just take the time to learn "this" and why it's important part of JavaScript.

This feels kind of like "I don't know how to open this door, so I'll build a latter on to the house that lets me get in the top window."

I will give you props on building an alternative solution.

Collapse
 
joelnet profile image
JavaScript Joel

I find your language of "excessive amounts of additional code" to be either an extreme exaggeration or a vast misunderstanding.

nothis is just a function. A SMALL function. The function takes 1 argument and returns 1 argument. It is one of the most simple of functions that you can call.

Overall in a project, nothis sets to REDUCE the amount of code written as well as COMPLETELY ELIMINATE an entire class of bugs!

And YES we should get rid of parts of the language that cause developers to run into bugs!

If there are two ways to write code, 1 probably doesn't have bugs and 1 definitely doesn't have bugs, why not choose the one that definitely doesn't have bugs?

NULL has been described as a Billion dollar mistake. this is far more complicated than the concept of NULL. How much will this cost us?

The solution of "Just learn it" is what we are currently doing. And it is NOT working!!!

A simple search of stack overflow or google will show there are MANY developers that get caught up on this.

How does the “this” keyword work?
I have noticed that there doesn't appear to be a clear explanation of what the this keyword is and how it is correctly (and incorrectly) used in JavaScript on the Stack Overflow site.

I have witnessed some very strange behaviour with it and have failed to understand why it has occurred.

How does this work and when should it be used?

There are many articles written to explain how this works. this is not an easy concept for a lot of developers. I think because many senior developers forget that.

And in cases of React, if we forget about the bugs that may come with this the nothis-react package will let us write much more compact code and even support argument destructuring!

import React from 'react'
import NoThis from 'nothis-react'

class Counter extends NoThis.Component {
  state = { count: 0 }

  increment({ setState }) {
    setState(({ count }) => ({ count: count + 1 }))
  }

  render({ increment, state: { count } }) {
    return (
      <div>
        <button onClick={increment}>{count}</button>
      </div>
    )
  }
}

Now that is SIMPLE code!

You may never agree with me, but please think of me the next time you write console.log(this) to debug this ;)

Collapse
 
dechamp profile image
DeChamp

in regards to your statement....

"I find your language of "excessive amounts of additional code" to be either an extreme exaggeration or a vast misunderstanding.

nothis is just a function. A SMALL function. The function takes 1 argument and returns 1 argument. It is one of the most simple of functions that you can call."


this is NOT a simple small function. It's a function that calls other functions and also includes lodash.

I honestly like what you did, it's clever and in some scenario could be helpful but I think you need to avoid writing an article that could lead new devs in to thinking that "this" is a horrible thing to use.

I feel more people would have been on board if you presented it in a manor of "hey, 'this' can be a headache but I have an alternative solution you can try".

I know it has to be frustrating to have an opinion and feel like people are shutting you down.
I'm not trying to say "you're wrong, I'm right!". Simply stating that if you present your argument as a "you are all wrong, this is the right way" then you will definitely meet some resistance.

Your library should be in production and used by thousands of developers before you start making it's a "suggested way". There are far too many scenarios where you're library could cause more harm than good and you are not even aware of those yet. Sure you'll fix them as them come, but you haven't had enough run time for you to even know what they are.

Keep up the good work, don't let our comments get ya down. Be open to what the community has to say. You rock. Have a good day.

Thread Thread
 
joelnet profile image
JavaScript Joel

this is NOT a simple small function. It's a function that calls other functions and also includes lodash.

Ahh very true, very true. Though it doesn't include the entire lodash library. Only the clone function is included. This was safer that writing my own clone function.

Your library should be in production and used by thousands of developers before you start making it's a "suggested way".

I'll agree with this too. But I'm stuck with a catch-22. Nobody will use it unless everyone is using it. So the only way to get everyone to use it is if everyone is using it.

Maybe a labeling of experimental? I'll think about this.

I know it has to be frustrating to have an opinion and feel like people are shutting you down.

Not at all. I understand the way comments or reviews work. If you are happy, you move on. If you disagree, straight to the comments! When people comment it gives me and opportunity to clear up any confusions that may exist!

The first thing I would teach is how to write software without the need for this. nothis should be a library when you are stuck with this due to circumstances out of your control.

Feedback much appreciated :)

Cheers!

Thread Thread
 
dechamp profile image
DeChamp

Thanks for taking the time to reply to everyone! I think people will use it and maybe even offer to add to it. I know half of the people I've talked to still struggle with this....

Thread Thread
 
joelnet profile image
JavaScript Joel

It's a longer conversation that goes beyond the scope of this article. Even though we've all read the Billion dollar mistake articles, we all continue to use NULL. Even I still do. I look at it now and grimace, but I still use it.

I know exactly how I would replace it in my code too. I'd use a Maybe with a Just and Nothing. But... in a lot of instances, I still don't. This is one of the next things I want to correct in my code. The elimination of any NullReferenceException.

There are a lot of things I feel this way about. Who knows. Maybe when I am done I won't be using anything except combinators. lol. (please no).

Until then all we can do is try to be better every day.

Cheers Amigo!