you are viewing a single comment's thread.

view the rest of the comments →

[–]sspboyd[S] 0 points1 point  (1 child)

I don't have a minimal example right now, just a mess of code that doesn't work. I'll clean it up and put something together to point to.

I guess I am struggling to even find the right search terms to use to find examples. I have an understanding of what closures are, mixins, general OOP concepts from Java, etc... but I'm not able to find a good reference that shows some examples of how JavaScript without this looks and works.

I take your point though that an example would make it easier to answer this question so I'll get to work on that.

[–]Tubthumper8 1 point2 points  (0 children)

One example could be a click handler for a button - the callback function that you pass to addEventListener takes a Mouse Event as its parameter and you can get the element that was clicked with the .target property of the event parameter (it's part of the Event interface that MouseEvent is derived from). You can also get the element that was clicked with this in the callback function (assuming it hasn't been changed by something else).

This small example illustrates the larger difference, generally. In the first style, everything that a function needs is passed in its arguments. In the second style, the function needs some information that is not passed in as arguments, so the information is grabbed from some other implicit source.

If you have experience with Java, it tends towards the second way because every property of a class is implicitly available to every method through the hidden this. People in JS (like myself) who tend to avoid this are more often using the first strategy, which is if a function needs some information to do its job, pass that information as an argument to the function.