When do I use "this"?


I have recently begun the lesson on the "this" keyword in JavaScript. I have been able to get through the lessons just fine, but I'm still a bit confused on when I am actually to use this keyword.

For example, I was able to use "this" when building a method from scratch, but not when creating objects for the method. I do not understand why this is or when exactly I can/ cannot use the "this" keyword. Can you explain?


The this keyword has been used in many languages. You should think of it as a reference to an object. You want to use it to make your methods usable by different objects (usually in the same class/subclass). When creating an object, you can use a this constructor to set values.

function create(this, val1, val2) {
this.val1 = val1;
this.val2 = val2;