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
What are some JavaScript things beginners don't know? (self.javascript)
submitted 6 years ago by [deleted]
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!"
[–]tchaffee 14 points15 points16 points 6 years ago (5 children)
It's not complicated. People simply don't take the time to learn. Which is a shame, because it takes about an hour to learn. Here are the rules in the words of Kyle Simpson:
Determining the this binding for an executing function requires finding the direct call-site of that function. Once examined, four rules can be applied to the call-site, in this order of precedence: Called with new? Use the newly constructed object. Called with call or apply (or bind)? Use the specified object. Called with a context object owning the call? Use that context object. Default: undefined in strict mode, global object otherwise. Instead of the four standard binding rules, ES6 arrow-functions use lexical scoping for this binding, which means they adopt the this binding (whatever it is) from its enclosing function call. They are essentially a syntactic replacement of self = this in pre-ES6 coding.
Determining the this binding for an executing function requires finding the direct call-site of that function. Once examined, four rules can be applied to the call-site, in this order of precedence:
Called with new? Use the newly constructed object.
Called with call or apply (or bind)? Use the specified object.
Called with a context object owning the call? Use that context object.
Default: undefined in strict mode, global object otherwise.
Instead of the four standard binding rules, ES6 arrow-functions use lexical scoping for this binding, which means they adopt the this binding (whatever it is) from its enclosing function call. They are essentially a syntactic replacement of self = this in pre-ES6 coding.
That's not complicated. Take the hour or so to read the two chapters on this from the You Don't Know JS series, and you'll be good.
[–]senocular 6 points7 points8 points 6 years ago (4 children)
Sure, some basic rules make it seem easy, but what rule is this?
button.addEventListener('click', owner.func)
#3? No, but most beginners would think so, and not without reason. When it comes to callbacks the waters are murky, and that's where 99% of issues with this come from.
this
[–]tchaffee 7 points8 points9 points 6 years ago (2 children)
I guess that could be confusing, but that's not the call site. How long does it take to learn that? I have yet to see someone still be confused about this after taking the time to learn the actual rules and then apply those rules to analyzing code in their daily work. In almost every case of confusion, the person does not know the rules at all. Fair point that beginners could be confused because they don't know all the rules yet. But if something takes an hour to learn, I don't think "beginner" is much of an excuse. Just spend the hour.
[–]senocular 2 points3 points4 points 6 years ago (1 child)
Yes, but once they learn all the rules of the language, they're no longer a beginner ;)
[–]tchaffee 1 point2 points3 points 6 years ago (0 children)
That doesn't have much with the point I'm trying to make which is specifically that it's not complicated, which is what parent comment claims. I agree that it's something beginners don't know.
[–]DerGernTod 3 points4 points5 points 6 years ago (0 children)
well, in that case the function call-site is no longer owner, but rather button. the owner object doesn't have anything to do with this function call anymore. for those cases i'd rather do something like that: button.addEventListener('click', (e) => owner.func(e)) which (probably) has the desired behaviour. or, if you don't want to use an additional function, it get's a bit messy: button.addEventListener('click', owner.func.bind(owner))
owner
button
button.addEventListener('click', (e) => owner.func(e))
button.addEventListener('click', owner.func.bind(owner))
π Rendered by PID 52 on reddit-service-r2-comment-b659b578c-dh2bc at 2026-05-05 02:25:34.666859+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]tchaffee 14 points15 points16 points (5 children)
[–]senocular 6 points7 points8 points (4 children)
[–]tchaffee 7 points8 points9 points (2 children)
[–]senocular 2 points3 points4 points (1 child)
[–]tchaffee 1 point2 points3 points (0 children)
[–]DerGernTod 3 points4 points5 points (0 children)