Search code examples
javascriptclasssyntax-erroridentifier

Uncaught SyntaxError: Unexpected identifier in JavaScript class at Function declaration


I get

Uncaught SyntaxError: Unexpected identifier

in the marked line below. I have no idea what to do. Tried all sorts of things but it seems like a fundamental mistake I have made.

class Order {

constructor(pVornameS, pNachnameS, pKlasseS, pVornameE, pNachnameE, pKlasseE) {
    this.vornameS = pVornameS;
    this.nachnameS = pNachnameS;
    this.klasseS = pKlasseS;
    this.vornameE = pVornameE;
    this.nachnameE = pNachnameE;
    this.klasseE = pKlasseE;
}

function getVornameS() { //The error occurs in this line
    return vornameS;
}

function getNachnameS() {
    return nachnameS;
}

function getKlasseS() {
    return klasseS;
}

function getVornameE() {
    return vornameE;
}

function getNachnameE() {
    return nachnameE;
}

function getKlasseE() {
    return klasseE;
}
}

Solution

  • You don't need the function keyword in there. Starting with ECMAScript 2015, a shorter syntax for method definitions on objects initializers is introduced. It is a shorthand for a function assigned to the method's name.