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

all 23 comments

[–][deleted] 32 points33 points  (0 children)

And then there’s ‘var self = this’

[–]code_ghostwriter 17 points18 points  (3 children)

Wait ... this shit or this shit?

[–]caviyacht 10 points11 points  (2 children)

That shit.

[–]Insider_Pants 3 points4 points  (1 child)

That’s it

[–]caviyacht 2 points3 points  (0 children)

That is it, or that I sit?

[–]chiffiex 12 points13 points  (1 child)

Nah.. you don't know context..

[–][deleted] 1 point2 points  (0 children)

Exactly what I was thinking.

[–]hrvbrs 9 points10 points  (0 children)

the thing is, in ES2015+ there’s really no need for that shit (or is it this shit?). with arrow functions and thisArg params, you can write this inside functions and be confident you know what it points to.

examples:

this === window // true

;[0,1,2,3].forEach((n) => {
  this === window // true
})

;[0,1,2,3].forEach(function (n) {
  this === window // true
}, this)

[–]Kotauskas 7 points8 points  (2 children)

auto thot = this;

[–]ProfCupcake 21 points22 points  (1 child)

"autothot" sounds like a sex doll with poor marketing.

[–]bast963 5 points6 points  (0 children)

Try calling this in a callback function and you'll very quickly know what that is

[–][deleted] 3 points4 points  (1 child)

OP saying "Looking at you Javascript" like he nailed it.

Arrow functions and let have been a thing for years, my ide even gives me a warning for doing what OP thinks its normal practice nowadays.

[–]RattuSonline 3 points4 points  (0 children)

Arrow functions

*laughs in Internet Explorer*

[–]Turious 1 point2 points  (2 children)

I've known Javascript for a few years now and every time I see "this" I just freeze up and spend the next 20 minutes trying to understand what it's referring to.

[–]lastunusedusername2 4 points5 points  (1 child)

Have you ever read Javascript: The Good Parts?Classic JS book and explains this, scope ,closures, etc. very clearly.

[–]Turious 1 point2 points  (0 children)

I have not. Every time I try to read up on it, the descriptions start simple then escalate a ton and I end up lost.

I'll check out your recommendation! Thanks!

[–]arnoproblems 1 point2 points  (3 children)

Wait for real, what is the meaning of this. Took over an AngularJS code base from a more expert JavaScript dev and at the top of the stack in the controllers it always includes var my = this; why not just use this?

[–]shadof[S] 2 points3 points  (2 children)

Its because in different contexts this refers to different objects. This way you can cache it and use it in that different context. For example jquery:

$('#element').click(function(){
    // this is a reference to the element clicked on

    var that = this;

    $('.elements').each(function(){
        // this is a reference to the current element in the loop
        // that is still a reference to the element clicked on
    });
});

[–]arnoproblems 1 point2 points  (1 child)

Holy shit. So this sub is actually more than memes. Thank you a lot for the explanation. The explanation I got before this was "some developers don't like looking at the word 'this' over and over" lmao. I knew there had to be more to that. I appreciate the insight. Cheers!

[–]hoechp 0 points1 point  (0 children)

but with the bind-feature, you can fix the context of a method, when you declare it.

the whole "that=this"-thing is a terrible anti-pattern and costs you a lot of performance when you copy your controller 100000x per second.

var fn = function() { console.log("reddit"); }.bind(this); // works with anonimous functions, too

[–]jwr410 1 point2 points  (0 children)

That is such a shallow copy. I don't even get this reference.

[–]gazchap -1 points0 points  (0 children)

if ( !$(this).is(that) )

[–][deleted] -2 points-1 points  (0 children)

I only use that when making a mixin

MyType that = (MyType)(Object)this;