在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ JavaScript中子類繼承父類,子類的一個實參是一個類,應(yīng)該如何寫const

JavaScript中子類繼承父類,子類的一個實參是一個類,應(yīng)該如何寫constructor函數(shù)

父類:

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;

子類:

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;

測試函數(shù)

"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("#introduce", () => {
            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("#introduceWith", () => {
            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.");

            });
});

用npm test運行后,提示:

 + 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.
回答
編輯回答
你的瞳

后來發(fā)現(xiàn)是另外一個文件里的類似代碼沒有修改,是另外一個文件報的錯

2017年6月14日 07:46
編輯回答
負(fù)我心
return `${super.introduce()} I am a Teacher. I teach Class ${this.klass}.`;

改為

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

XXX是什么得看你的Class定義

2018年7月5日 06:52