+ 1
What is the point of this.name=name?
I don't understand the reason for the three parts (this, name, name). What is the difference between the 2 name parts? Thank you!
1 Resposta
0
I am largely unfamiliar with JavaScript, but that looks similar to Python Object-Oriented Programming. AFAIK, you wouldn’t say “this” twice in a method definition, I think that was a mistake judging by the title. If that is a method definition like I think, then the reason to assign the value from “name” to “this.name” is that now the value of “name” isn’t just in the method scope, it’s in the object scope. “this.name” means “the name attribute of this object”. Any other method of the object can now reference “this.name” at any time without “name” needing to be passed as an argument again.
Hope this helps!