22/11/11_TIL TS예습

강해경·2022년 11월 21일

Today I Learned

목록 보기
23/36
class Word {
  constructor(
  public term: string, 
  public def: string) {}
} 

const kimchi = new Word('kimchi', '한국음식');
console.log(kimchi);
// Word { term: 'kimchi', def: '한국음식'}
"use strict";
class Word {
    constructor(term, def) {
        this.term = term;
        this.def = def;
    }
}
type Words = {
  [key: string]: string;
};

class Dict {
  private words: Words;
  constructor() {
    this.words = {};
  }
  add(word: Word) {
    if (this.words[word.term] === undefined) {
      this.words[word.term] = word.def;
    }
  }
  def(term: string) {
    return this.words[term];
  }
}
const myDic = new Dict();
console.log(myDic); // Dict { words: {} }
myDic.add(kimchi);
console.log(myDic); // Dict { words: { kimchi: '한국음식' } }
console.log(myDic.def('kimchi')); // 한국음식

0개의 댓글