Thanks for this article! For get set I think it's more customary to use get name() and set name() instead of get getName() set setName(), but it's essentially the same.
class Person {
constructor(name) {
this._name = name;
}
get name() {
return this._name;
}
set name(name) {
this._name = name;
}
}
let p = new Person("willie")
p.name = "jimmy"
console.log(p.name)
I'd like to point to the case of your get Name and set Name. Usually you keep those methods in lowercase to simulate an actual property so that who uses your class/object sees them as normal property retrieval/assignment.
@eerk
example is perfect, since it is also good practice to name the actual property with the same name of the getter/setter methods prepended by an underscore
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Thanks for this article! For
get set
I think it's more customary to useget name()
andset name()
instead ofget getName() set setName()
, but it's essentially the same.Thank you. I updated :) :)
I'd like to point to the case of your
get Name
andset Name
. Usually you keep those methods in lowercase to simulate an actual property so that who uses your class/object sees them as normal property retrieval/assignment.@eerk example is perfect, since it is also good practice to name the actual property with the same name of the getter/setter methods prepended by an underscore