Home > Blockchain >  SyntaxError: 'super' keyword unexpected here when extending superclass
SyntaxError: 'super' keyword unexpected here when extending superclass

Time:05-08

I have a super class School with properties name,level, and number of students

class School{
      constructor(name,level,numberOfStudents){
        this._name = name;
        this._level = level
        this._numberOfStudents = numberOfStudents
      }
      get name(){
        return this._name;
      }
      get level(){
        return this._level
      }
      get numberOfStudents(){
        return this._numberOfStudents
      }
    }// end School() class 
    

But when I extend that to a subclass PrimarySchool...

    class PrimarySchool extends School{
      constructror(name,numberOfStudents,pickupPolicy){
        super(name,'primary',numberOfStudents)
        this._pickupPolicy = pickupPolicy
      }
      get pickupPolicy(){
        return this._pickupPolicy
      }
    }//end PrimarySchool

I get SyntaxError: 'super' keyword unexpected here. But I should be able to pass in 'primary' as the value for the parent class property level. Same with name and numberOfStudents. Even If I declare level in the sub-class constructor and don't even pass in a value, I get the same error:

class PrimarySchool extends School{
  constructror(name,level,numberOfStudents,pickupPolicy){
    super(name,level,numberOfStudents)
    this._pickupPolicy = pickupPolicy
  }
  get pickupPolicy(){
    return this._pickupPolicy
  }
}//end PrimarySchool 

CodePudding user response:

There is a typo in the constructror in the PrimarySchool class. The correct word is constructor. Here's a working sample code:

class School{
      constructor(name,level,numberOfStudents){
        this._name = name;
        this._level = level
        this._numberOfStudents = numberOfStudents
      }
      get name(){
        return this._name;
      }
      get level(){
        return this._level
      }
      get numberOfStudents(){
        return this._numberOfStudents
      }
    }// end School() class 
    

   class PrimarySchool extends School{
      constructor(name,numberOfStudents,pickupPolicy){
        super(name,'primary',numberOfStudents)
        this._pickupPolicy = pickupPolicy
      }
      get pickupPolicy(){
        return this._pickupPolicy
      }
    }//end PrimarySchool


const school = new PrimarySchool( 'NY School', 2137, true );

console.log( school );

Do you use any IDE with syntax highlighting or a linter (e.g. ESLint)?

  • Related