라고 생각했는데 진짜 맞네?
자바스크립트는 동적타이핑 언어였다면
타입스크립트는 정적타이핑 이라는 소리
// JS
let isDone = false;
let amount = 20;
let userName = "Alice";
function multiply(a, b) {
return a * b;
}
console.log(multiply(5, 3));
// TS
let isDone: boolean = false;
let amount: number = 20;
let userName: string = "Alice";
function multiply(a: number, b: number): number {
return a * b;
}
console.log(multiply(5, 3));
난 자유로운게 좋고 코드들도 자유롭고 싶을텐데 왜 자유를 침범해?
어떻게 변수 하나하나에 Type을 먹이고 명시적으로 인생을 정해줘야 하는데?
이 아이들도 인생이 있고 자기들이 가고싶은 방향을 순수하게 정하고 싶을거 아냐😠
라고하면 안되겠죠?
클래스와 상속 JS와 TS 비교
// JS
function Animal(name) {
this.name = name;
}
Animal.prototype.speak = function () {
console.log(`${this.name} makes a noise.`);
}
function Dog(name) {
Animal.call(this, name);
}
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Dog.prototype.speak = function () {
console.log(`${this.name} barks.`);
}
let dog = new Dog('Rex');
dog.speak();
// TS
class Animal {
name: string;
constructor(name: string) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a noise.`);
}
}
class Dog extends Animal {
constructor(name: string) {
super(name);
}
speak() {
console.log(`${this.name} barks.`);
}
}
let dog = new Dog('Rex');
dog.speak();
근데 JS도 constructor(생성자) 사용하는데 왜 안넣어줬는지 의문
[TIL] 2024.02.20 트러블슈팅 (0) | 2024.02.21 |
---|---|
[TIL] 2024.02.19 jest 디버깅 오류 (0) | 2024.02.19 |
[TIL] 2024.02.15 sparpet 발표 후 회고 (2) | 2024.02.15 |
[TIL] 2024.02.14 sparpet 트러블슈팅 (0) | 2024.02.15 |
[TIL] 2024.02.14 passport-kakao (0) | 2024.02.14 |