====== class ====== javascriptには厳密な型がない。そのためinterfaceはないが、interfaceっぽいことは簡単にできる。 export class Animal{ constructor(name){ this.name = name } cry(){ return "cry!" } } export class Cat extends Animal{ constructor(name){ super(name) } cry(){ return "nyaa" } } export class Dog extends Animal{ constructor(name){ super(name) } cry(){ return "bau" } } export default class Human{ constructor(pet){ this.pet = pet } doCry(){ return this.pet } } import {Animal,Cat,Dog} from '../Animal' import Human from '../Human' describe("Extends",()=>{ test("Animal",()=>{ let a = new Animal() expect(a.cry()).toBe("cry!") }) test("Cat",()=>{ let a = new Cat("tama") expect(a.cry()).toBe("nyaa") }) test("Pet",()=>{ // Humanはpetというメンバ変数を持っている。 // pet(という型はない)にAnimalを継承したCatを設定しCatのメソッドを呼び出している。 let minako = new Human(new Cat("tama")) expect(minako.doCry().cry()).toBe("nyaa") }) })