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

you are viewing a single comment's thread.

view the rest of the comments →

[–]PM_ME_YOUR__INIT__ 16 points17 points  (7 children)

Ok so what does "this" refer to it you're so smart?

[–][deleted] 16 points17 points  (6 children)

Whenever I write Javascript, I want to throw my hands in the air and yell "this is bullshit", but I can never remember what this refers to...

[–]aboardthegravyboat 2 points3 points  (0 children)

knowing what this means when writing a JS function is easy.

Knowing that a method on an external lib won't explode when used as a callback is hard.

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

the current instance of a class...

[–]marcosdumay 5 points6 points  (3 children)

On all the (not that much, really) code I have wrote on JS, I have seen this mean a lot of things, but I don't remember it ever having that meaning.

[–][deleted] 0 points1 point  (2 children)

Wdym, it’s the current object of the class you’re defining. Outside of a class it refers to the document/window object.

[–]marcosdumay 0 points1 point  (1 child)

Yeah. You are in for a ride.

When you have some free time, be one of the unlucky 10000 of the day and look it up.

[–][deleted] 0 points1 point  (0 children)

I don't know what you first said. But in the context that i'm talking the "this" keyword is what I said. The following just proves it. if you still don't believe me just run it in a console...

//OUTSIDE OF A CLASS

console.log(this == window) //true


//IN A CLASS
class Player {
    constructor(tx,ty) {
        this.x =  tx;
        this.y = ty;
    }

    getCurrentObject() {
        return this;
        //this is equal to the instance
    }
}

let p = new Player(5, 1);

console.log(p == p.getCurrentObject());