This forum is now read-only. Please use our new forums at

1292 points
Submitted by
Michael Oles
almost 6 years ago

Can someone explain how "This" works?

Maybe there is already another forum post on this but I don't understand how "This" works. I know that this is substituted with something else but how does the computer know what to substitute into the spot of "this?

1 vote


The computer knows what to put instead of "this" because the object you are using.
Im not an expert but here is an example:

Lets suppose that we have an object (myObj) that has a method (myMet):
This is the method

   myMet = function(){
    console.log("Hi my name is "+;  

This is the Object


In this case, the computer knows that "this" will be changed for "myObj" because the thing that is using the method, in this case "myObj". So, "this" will help you to use this method with other objects.


Here, "this" will be changed by "myObj2".

Sorry for my english problems :P I HOPE it helps :)

554 points
Submitted by
Kiven Cortes
almost 6 years ago

1 vote


this is powerful and strange and confusing in JS.

var obj = {
    firstName: "Fred"
    // Test object
    var obj2 = {
    firstName: "James"
    }; = function() {
}; = function() {

this in line 9 is saying (what did you call last?)

  • In this instance, this refers to "Fred". Why? We called foo for the target object.
  • We told it to find this obj's firstName value.

This can be expanded greatly. But, the fact of the matter is that it demonstrates this quite well in a basic sense.

View here for more information:

2126 points
Submitted by
Nicholas Hazel
over 5 years ago