Particularly in JavaScript object oriented programming:
If I have a class like this, for example:
class Animals{
constructor(name, species){
this.name = name;
this.species = species;
}
}
Why do we need to use this.yyyy = yyyy in the constructor? I have read explanations many times but still can’t get it, please explain like I’m 5
In: 28
I’m surprised I haven’t seen this comment yet but we don’t need “this” keyword at all. We don’t even need constructors. In fact, as far as I know, no computer ever has ever had the concept of “classes” built into its instruction set.
So “this” keyword, as well as constructors, as well as classes and OOP in general, is an abstraction. It’s an artifact of the language you are using. But the underlying bare metal you are running your code on doesn’t see or care about any of that. So in short we don’t need it at all to run code, it’s just a nice feature of a language for the purposes of clarity
Latest Answers