When you think of the keyword this you probably assume it refers to the current instance of the class. This is true for most object oriented programming like C# and Java.
For example I could use the this keyword in C# like this:
class Cat {
string _name;
public Cat(string name) {
this._name = name;
}
}
In the above example you see this illustrated. In C# and Java, this always refers to the class instance.
So, knowing this you would probably assume the same would be true of object oriented programming in JavaScript and it's this keyword. This is, however, not the case. Like a lot of things about writing object oriented code in JavaScript, this behaves differently in some situations. this does not always refer to the class instance depending on how you use it.
function Cat(name) {
this.Name = name;
}
In the above object oriented programming example it works just like our C# example but look at a situation where things can go wrong if you are unaware of some rules.
(
Read more
)