I am trying to understand how OOP stuff work in JavaScript. Coming from a java background I am used to creating classes as follows,
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void getName(String name) {
this.name = name;
}
}
Now I am writing the same class in JavaScript in the following way
class Person {
constructor(name) {
this.name = name;
}
get getName() {
return this.name;
}
set setName(name) {
this.name = name;
}
}
Now what I am expecting is that the variable "name" in the class to be only accessed via the setter and getter functions. However when I am able to change change the value of the name just simply by using the dot notation. Is there way of making the variable only to be accessed by the functions?