内容へ移動
猫型iPS細胞研究所
ユーザ用ツール
ログイン
サイト用ツール
検索
ツール
文書の表示
以前のリビジョン
バックリンク
最近の変更
メディアマネージャー
サイトマップ
ログイン
>
最近の変更
メディアマネージャー
サイトマップ
現在位置:
INDEX
»
javascript
»
class
トレース:
javascript:class
この文書は読取専用です。文書のソースを閲覧することは可能ですが、変更はできません。もし変更したい場合は管理者に連絡してください。
====== class ====== javascriptには厳密な型がない。そのためinterfaceはないが、interfaceっぽいことは簡単にできる。 <code javascript Anmimal.js> 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" } } </code> <code javascript Human.js> export default class Human{ constructor(pet){ this.pet = pet } doCry(){ return this.pet } } </code> <code javascript Animal.test.js> 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") }) }) </code>
javascript/class.txt
· 最終更新: 2019/03/05 23:55 by
ips
ページ用ツール
文書の表示
以前のリビジョン
バックリンク
文書の先頭へ