(숙제2) class인데 파라미터가 잔뜩 들어가는 class Word를 만들어봅시다.
겁쟁이는 아니고 문제는 다 풀었습니다.
class Word {
num: number[];
str: string[];
constructor(...param: (number | string)[]) {
this.num = [];
this.str = [];
param.forEach((v) => {
if(typeof v === 'number') {
this.num.push(v);
} else if(typeof v === 'string') {
this.str.push(v);
}
})
}
}
let obj = new Word('kim', 3, 5, 'park', 31, 28, 5, 8, 9, 13, 'j', 'holy', 'shit');
console.log(obj.num) //[3,5]
console.log(obj.str) //['kim', 'park']
저는 위와 같이 풀었는데 선생님은 constructor 안에 let으로 변수를 넣어주셨더라고요.
정답은 똑같이 잘 나오는데 선생님의 코드에 어떤 장점이 있는지 궁금합니다.