[英]Javascript SyntaxError: Unexpected Token '('
I'm having a strange issue when defining a method named get
or set
inside a class. 在类中定义名为
get
或set
的方法时遇到一个奇怪的问题。 For example, I am defining the class Mem as follows: 例如,我将类Mem定义如下:
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 '('
但我收到一个
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. 但是,当我将方法名称更改为
geta
或seta
,该类定义起作用并且没有得到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. 我检查了
get
和set
不是Javascript中的受限关键字,因此我对自己做错了感到困惑。
You are colliding with the getter and setter syntax . 您正在与getter和setter语法冲突。
{get prop() { ... } }
{get [expression]() { ... } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.