+ 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!

16th May 2018, 10:39 PM
Watchtower
1 Réponse
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!
13th Apr 2024, 4:28 PM
Wilbur Jaywright
Wilbur Jaywright - avatar