r/javascript • • Feb 21 '18

I am A JavaScript instructor at Microsoft. AMA.

Hi r/javascript!

My name is Benjamin Lin. I am a content developer at Microsoft and teach courses on JavaScript and JavaScript frameworks at Microsoft. You can see my Top 10 JavaScript Questions course here.

I can help answer tips & tricks on learning JavaScript, questions about Microsoft, and all things tech.

AMA!

Proof.

Edit: about 15 minutes left!

More edit: Thanks everyone for visiting me at this AMA! I really enjoyed getting the chance to interact with the JavaScript community and hear your honest thoughts. It was worth waking up early today to do this =)

if you have any more questions, feel free to hit me up on twitter @benjlin or follow me on instagram @bens_jams

540 Upvotes

257 comments sorted by

View all comments

Show parent comments

1

u/[deleted] Feb 22 '18

I don't think the confusion necessarily stems from the concept of 'this', but more how in JS 'this' can be all over the place. Scope is funky. Using an arrow function leads to a different scope, functions as parameters is screwy, jQuery is just...jQuery.

I can't keep it all straight, personally. So I just avoid using this. as much as possible.

1

u/[deleted] Feb 22 '18 edited Feb 22 '18

Using an arrow function leads to a different scope

No it doesn't. Arrow functions retain their parent scope. Using a function necessarily switches your scope up, unless you bind it.

functions as parameters is screwy

Alternately, it's a feature that's present far back as C, and is the entire basis for Haskell. Lambda calculus is an important part of knowing how to program. Only OOP stuff like Objective C do the weird "let me encapsulate this simple callback in a delegate class" nonsense - even Java saw the error of its ways and implemented immediate closures.

jQuery is just...jQuery

Then don't use it. It's basically unnecessary in modern browsers. Sure, querySelectorAll is a lot to type, as is setAttribute, but you can always make local aliases. [].forEach and [].map are terribly useful. JSON is ubiquitous. fetch is a whole character terser than $.ajax, a lot more powerful, and, oh, does Promises correctly. Meanwhile, if you're doing your animations with javascript anymore, you've either got weird requirements or you're doing it wrong.

0

u/[deleted] Feb 22 '18

Arrow functions have 'this' lexically scoped , declared functions based on where they're called. That's different. And it doesn't even cover strict mode, objects, constructors, working with the DOM, bind/call/apply...

And by functions as parameters, I was speaking - again - to 'this' scoping. But while we're at it...in the middle of a conversation about explanation of 'this' to beginners you want to drop a bunch of opinionated high-level knowledge? Hmm...

And I brought up jQuery because a lot of beginner JS programmers have experience with it. How it handles 'this' might confuse them. I - fortunately - work in node and not the browser :)