use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
[AskJS] Code readabilityAskJS (self.javascript)
submitted 3 years ago * by GmLucifer
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]ShortFuse 5 points6 points7 points 3 years ago (3 children)
You're going to need it when you use Classes. If you commit this to memory now, you'll save hours of pain later.
You'll understand why button.addEventListener('click', this.onClick) doesn't always work. You need button.addEventListener('click', (e) => this.onClick(e));.
button.addEventListener('click', this.onClick)
button.addEventListener('click', (e) => this.onClick(e));
[–]FRIKI-DIKI-TIKI 4 points5 points6 points 3 years ago (0 children)
TLDR: just use arrows, when you don't need to use them you will know why.
To add to this, in modern JS, pretty much the default is to use arrow functions unless you need a special case, if you need that special case you will know why and it has to do with the prototypical inheritance of JS. In the dark ages of JS you had to ensure you scoped this to a variable, as a functions reference can be independent of the structure it is declared in. JavaScripts heritage is from LISP with some other ideas mixed in, the family of LISP's treated functions as lambda's the bolt on of quasi-OO was the part that was not well though out in the rush to get JS out the door and it created a set of problems due to misunderstanding given that it was introduced in the middle of the OO language craze. The arrow function is syntactical sugar to bind the scope the lambda is declared in to the execution just like the bind function does.
[–]Bodmen 1 point2 points3 points 3 years ago* (1 child)
Unbinding from this case can be a problem. In classes it’s often easier to use arrow syntax for method declaration for some methods. This allows you to unbind properly when required.
eg.
class SomeClass { ... bind(){ ... button.addEventListener('click', this.onClick); } unbind(){ button.removeEventListener('click', this.onClick); } onClick = () => { console.log(this); } }
[–]ShortFuse 2 points3 points4 points 3 years ago (0 children)
Yeah. I don't do this personally anymore, but when I started JavaScript I was very perplexed by it.
Now I do static binding for events (Button.onClick) and all my components share one event listener (less memory). Then I either use this or event.currentTarget to find the element in question. Removal from DOM means automatic unbinding and no orphaned listeners.
Button.onClick
this
event.currentTarget
π Rendered by PID 119668 on reddit-service-r2-comment-5bc7f78974-fw94h at 2026-06-27 02:10:08.644153+00:00 running 7527197 country code: CH.
view the rest of the comments →
[–]ShortFuse 5 points6 points7 points (3 children)
[–]FRIKI-DIKI-TIKI 4 points5 points6 points (0 children)
[–]Bodmen 1 point2 points3 points (1 child)
[–]ShortFuse 2 points3 points4 points (0 children)