The JavaScript subclass inherits the parent class, and one argument of the subclass is a class, how to write the constructor function

parent class:

class Person{
    constructor(name,age){
        this.name = name;
        this.age = age;
    }

    introduce(){
        return `My name is ${this.name}. I am ${this.age} years old.`;
    }
}

module.exports = Person;

subclass:

import Person from "../../src/practice_7/person.js"

class Teacher extends Person{
    constructor(name, age, klass){
        super(name, age);
        this. klass = klass;
    }

    introduce(){
        if(this.klass === undefined){
            return `${super.introduce()} I am a Teacher. I teach No Class.`
        }else{
            return `${super.introduce()} I am a Teacher. I teach Class ${this.klass}.`;
        }
        
    }

    introduceWith(studentJerry){
        if(studentJerry.klass === this.klass){
            return `${super.introduce()} I am a Teacher. I teach Jerry.`
        }else{
            return `${super.introduce()} I am a Teacher. I don"t teach Jerry.`
        }
    }
}

module.exports = Teacher;

Test function

"use strict";
import _ from "lodash";
import chai from "chai";
import sinon from "sinon";
import sinonChai from "sinon-chai";
const expect = chai.expect;
chai.use(sinonChai);

import Person from "../../src/practice_7/person.js";
import Student from "../../src/practice_7/student.js";
import Teacher from "../../src/practice_7/teacher-option2.js";
import Class from "../../src/practice_7/class.js";       

    describe("Teacher", () => {
        let klass;

        before(() => {
            klass = new Class(2);
        });

        it("should have field name, age and class number", () => {
            const teacher = new Teacher("Tom", 21, klass);
            expect(teacher.name).to.equal("Tom");
            expect(teacher.age).to.equal(21);
            expect(teacher.klass).to.equal(klass);
        });
     });

        describe("-sharpintroduce", () => {
            it.only("should overwrite Person introduce, introduce with name, age and class number, given teacher have class", () => {
                const teacher = new Teacher("Tom", 21, klass);
                const introduce = teacher.introduce();

                expect(introduce).to.equal("My name is Tom. I am 21 years old. I am a Teacher. I teach Class 2.");

            });

            it("should overwrite Person introduce, introduce with name, age and class number, given teacher have no class", () => {
                const teacher = new Teacher("Tom", 21);
                const introduce = teacher.introduce();

                expect(introduce).to.equal("My name is Tom. I am 21 years old. I am a Teacher. I teach No Class.");

            });
        });

        describe("-sharpintroduceWith", () => {
            let studentJerry;

            before(() => {
                studentJerry = new Student("Jerry", 8, klass);
            });

            it("should return I am teaching some guy, given my class is same with this guy"s class", () => {
                const teacher = new Teacher("Tom", 21, klass);
                const introduce = teacher.introduceWith(studentJerry);

                expect(introduce).to.equal("My name is Tom. I am 21 years old. I am a Teacher. I teach Jerry.");

            });

            it("should return I am teaching some guy, given my class is different with this guy"s class", () => {
                const teacher = new Teacher("Tom", 21, new Class(10));
                const introduce = teacher.introduceWith(studentJerry);

                expect(introduce).to.equal("My name is Tom. I am 21 years old. I am a Teacher. I don"t teach Jerry.");

            });
});

after running with npm test, prompt:

 + expected - actual

      -My name is Tom. I am 21 years old. I am a Teacher. I teach Class [object Object].
      +My name is Tom. I am 21 years old. I am a Teacher. I teach Class 2.
Mar.17,2021

return `${super.introduce()} I am a Teacher. I teach Class ${this.klass}.`;
Change

to

return `${super.introduce()} I am a Teacher. I teach Class ${this.klass.XXX}.`;

what XXX is depends on your Class definition


later found that the similar code in another file had not been modified. It was an error reported by another file

.
Menu