I'm having a strange issue when defining a method named get
or set
inside a class. For example, I am defining the class Mem as follows:
class Mem {
constructor() {
this.list=[]
}
get(index) {
return this.list[index];
}
set(index, value) {
this.list[index] = value;
}
}
But I get a SyntaxError: Unexpected token '('
However, when I change the method name to geta
or seta
, the class definition works and I don't get the SyntaxError.
class Mem {
constructor() {
this.list=[]
}
geta(index) {
return this.list[index];
}
seta(index, value) {
this.list[index] = value;
}
}
I checked that get
and set
were not restricted keywords in Javascript, so I am confused as to what I am doing wrong.