type Words = {
[key: string]: string
// key의 이름은 모르지만 타입을 알 때 이렇게 작성!
}
// let dict: Words = {
// "potate": "food"
// }
class Dict {
private words: Words
constructor() {
this.words = {}
}
Add(word: Word) {
if (this.words[word.term] === undefined) {
this.words[word.term] = word.def
}
}
Get(term: string) {
return this.words[term] ? this.words[term] : 'none';
}
Update(word: Word) {
if (this.words[word.term]) {
this.words[word.term] = word.def;
}
}
Del(term: string) {
if (this.words[term] === undefined) {
console.log("nothing to delete");
} else {
delete this.words[term];
console.log("success to delete");
}
}
All() {
return this.words;
}
}
class Word {
constructor(
public term: string,
public def : string,
){}
}
const kimchi = new Word("kimchi", "한국의 음식")
const newKimchi = new Word("kimchi", "한국의 전통음식")
const dict = new Dict();
dict.Add(kimchi);
console.log(dict.Get("kimchi"));
dict.Update(newKimchi);
console.log(dict.Get("gam"));
console.log(dict.All());
dict.Del("kimchi");
console.log(dict.All());
뚜든... ts 공부하면서 처음 알게 된
delete
연산자...;;;
객체 안에 있는 속성을 삭제할 때 사용하는 연산자인데 이제 알게 됐다 빡댁얼 ㅠ 메모메모~
// example from 'MDN DOCS'
const Employee = {
firstname: 'John',
lastname: 'Doe'
};
console.log(Employee.firstname);
// expected output: "John"
delete Employee.firstname;
console.log(Employee.firstname);
// expected output: undefined