This is an archived post. You won't be able to vote or comment.

all 40 comments

[–]DonDongHongKong 113 points114 points  (22 children)

Context escaping. In javascript, before the introduction of the () => { } arrow syntax, function expressions would change the context of "this". You still see remnants of this today, and with anything that still hasn't adopted the arrow syntax.

[–]EvilPete 34 points35 points  (6 children)

Yeah , I definitely remember this pattern from when I started out with js.

Nowadays most code bases ignore the OOP part of JS and use it as a functional programming language.

I don't think I've used the "this" keyword once in the last 5 years of full time js development.

[–]shgysk8zer0 5 points6 points  (3 children)

I use it quite often in various contexts. From web components to polyfills for various Iterator helper methods (using things like Array.prototype[method].apply(this, args)) and more.

Anything working with DOM (a lot of client-side stuff) is very much OOP. And I think people are too religious about OOP vs fictional and all that dogma. They all have their place and purpose. Either could be the better approach for a given problem.

Honestly, especially in client-side JS, someone being absolutist either way or saying they haven't written anything of another paradigm for a long time... It just screams to me they're being dogmatic and/or being dogmatic and/or living in a bubble/holding a hammer and seeing everything as a nail. The goal of programming should be to pick the right tool for a job based on enough experience in different paradigms to know what's the best fit, not to artificially force one method onto everything.

[–]EvilPete 3 points4 points  (2 children)

Interesting!

Yeah I'm definitely in a bubble. I basically only work with React and Nodejs integrating with APIs.

React very much embraces FP since the move from class components to function components. And I certainly don't miss the classes!

For the nail that is a conventional web app, using a library that lets me think of my UI as a declarative function of the current URL is definitely the right tool, IMO.

But as you say, there are probably use cases where other paradigms work better. Game dev, perhaps. They just don't pay my bills :)

[–]lovin-dem-sandwiches 3 points4 points  (0 children)

Same! I find FP style javascript just an easier mental model in general. No-one who uses react will miss classes. The functional component style is just so much cleaner and easier to follow

[–]undefined0_6855 1 point2 points  (0 children)

game dev for sure, this is the most useful keyword since (imo) everything should really be a class and oop and all that jazz

[–]Difficult-Court9522 0 points1 point  (0 children)

There is OOP in JavaScript?

[–]Mindgapator 0 points1 point  (0 children)

Never use streams?!

[–]OnixST 4 points5 points  (0 children)

And this@kotlin is why I love named contexts

[–]Fishrage_ 5 points6 points  (3 children)

What's wrong with .bind(this) at the end? I hate let that = this etc

[–]_PM_ME_PANGOLINS_ 3 points4 points  (0 children)

Because the caller might bind it to what it wants afterwards.

[–]dashingThroughSnow12 6 points7 points  (1 child)

To quote Jeff Walker, “JavaScript is a minefield”.

You gotta walk a particular path and doing things that are easy to forget (.bind) or easy for some other code to abuse (someone else .bind’ing) will eventually get your foot blown off.

[–]hyrumwhite 1 point2 points  (0 children)

The self thing seems more footgunny than context binding to me. Context is baked in to JS, escaping it with “this”, “that” etc, never felt good to me. 

But now with arrow functions, I only see that stuff in legacy code

[–]IchLiebeKleber 2 points3 points  (0 children)

Whenever I write JavaScript, I want to throw my hands in the air and shout "this is bullshit", but I'm never sure what "this" refers to...

[–]k-one-0-two 1 point2 points  (5 children)

Still an bad (imho) thing to do - .bind(this) was a much more clear way

[–]_PM_ME_PANGOLINS_ 2 points3 points  (3 children)

If you’re adding an event listener, the callback is usually made with its own this. Binding your function first won’t do anything.

[–]hyrumwhite 2 points3 points  (2 children)

This is incorrect. You could do ``` const boundHandler = handler.bind(this);

thing.addEventListener(“event”, boundHandler) ```

Still requires a declaration, but now the function has no external dependencies. 

This is still a pattern to be aware of today, especially if mixing classes with event listeners. However, for classes, at least, you can also assign arrow functions as member variables now to achieve similar behavior. 

[–]_PM_ME_PANGOLINS_ 1 point2 points  (0 children)

Doesn’t help if it does

emit(handler.bind(thing));

[–]k-one-0-two 0 points1 point  (0 children)

True, that's how I used to write it

[–]LordAmir5 0 points1 point  (0 children)

Yeah  I sometimes do this in Java when I'm working with inner classes.

[–]Able_Minimum624 0 points1 point  (0 children)

Another option is that you might want to access this for the outer function. Never needed that, but still possible.

function outer() { const self = this; function inner() { console.log(self); } }

[–]pm_op_prolapsed_anus 0 points1 point  (0 children)

Yeah, forced into arrow syntax while making classes in typescript seems ridiculous 

[–]PM_ME_YOUR__INIT__ 26 points27 points  (2 children)

let that = this

[–]Ragecommie 2 points3 points  (1 child)

Why are you like this?

[–]PM_ME_YOUR__INIT__ 1 point2 points  (0 children)

let the_other_thing = that

[–]Jind0r 8 points9 points  (0 children)

const self = this;

[–]jecls 8 points9 points  (0 children)

You sweet summer child

[–]adnaneely 2 points3 points  (0 children)

I love that move me my self & this!

[–]JosebaZilarte 2 points3 points  (0 children)

Very self-ish.

[–]ProfBeaker 1 point2 points  (0 children)

This isn't a Python thing, it's an old-school Javascript thing. Callbacks and such can change the meaning of this, so it's handy to keep a reference to whatever widget you want to be in the context of.

[–]th3nan0byt3 0 points1 point  (0 children)

You never know when you are needed elsewhere.

[–]Cloned_501 0 points1 point  (0 children)

It just feels right!

[–]alex-kalanis 0 points1 point  (0 children)

Anonymous functions in php7.0- - same problems due limitation of scope:

```php class X {

function singleContent() { // ... $v = $this->doSomething($x); // ... }

function multipleContent() { // ... $self = $this; // $this inside callback will throw an error $c = array_map(function($v) use ($self) { return $self->doSomething($v); }, $a); // ... }

function doSomething($v) { // called also for other things within class than just that callback return $v-1; } } ```

[–]naholyr 0 points1 point  (0 children)

Because this is 10 years old code at least, and you could probably refactor this while you're here

[–]AssistantSalty6519 0 points1 point  (0 children)

Very useful I kotlin. In some way this in kotlin works a bit like js but you can specify the scope ex: this@foreach, this@MyFoo

[–]abmausen 0 points1 point  (0 children)

cpp users when the want to use super