简体   繁体   English

Javascript SyntaxError:意外令牌'('

[英]Javascript SyntaxError: Unexpected Token '('

I'm having a strange issue when defining a method named get or set inside a class. 在类中定义名为getset的方法时遇到一个奇怪的问题。 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. 但是,当我将方法名称更改为getaseta ,该类定义起作用并且没有得到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. 我检查了getset不是Javascript中的受限关键字,因此我对自己做错了感到困惑。

You are colliding with the getter and setter syntax . 您正在与getter和setter语法冲突。

{get prop() { ... } }
{get [expression]() { ... } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM