-
[TypeScript] Class 문법 Type지정하기공부/TypeScript 2024. 3. 21. 16:52
Class 문법이란?
- ES6에서 추가된 문법으로 좀 더 코드를 객체지향적으로 표현하기 위해 추가된 문법이다.
- TypeScript의 class 문법 type 지정은 다음과 같다.
//TypeScript 타입지정 문법 class Product { //constructor을 쓸때는 위에서 변수를 미리 선언하고, type을 지정한다. name : string; price : number; /* 기억할 포인트 1. constructor안에서도 파라미터의 타입 지정이 가능하다. 2. 생성된 데이터는 항상 object 이기 때문에 return 타입을 지정할 필요가 없다. 3. 함수와 관련된 모든 문법이 적용가능하다. */ /* cunstructor (생성함수, 생성자) 객체를 정의하면, 그 객체에 의해 생성될 instance(인스턴스)의 프로토타입이 자동으로 생성된다. */ constructor(name : string, price : number){ this.name = name; this.price = price; } // Class 안에는 함수도 작성이 가능하다. // prototype 함수 넣는 자리로 return 있다면 return도 타입지정하기 Pt_function(name : string, price : number) : string { return `${this.name} 상품의 가격은 ${this.price} 입니다.` } } let Prouduct1 = new Product('RTX3080', 400000); console.log(Prouduct1); // Product {name: 'RTX3080', price: 400000} console.log(Prouduct1.Pt_function('RTX3080', 400000)); //RTX3080 상품의 가격은 400000 입니다.
Class/Prototype 문법을 모른다면 우선 문법을 이해해야한다.
[Javascript] Class 문법
Class 문법이란? ES6에 추가된 문법으로 java스럽게 객체지향적으로 표현하기 위한 새로운 문법을 의미한다. Object를 복사할 때 유용한 문법 유용하다. class Car { /* constructor는 인스턴스를 생성하고
studychickenman.tistory.com
'공부 > TypeScript' 카테고리의 다른 글
[TypeScript] Rest / Spread / Destructuring 문법 (1) 2024.03.24 [TypeScript] Interface 란? (0) 2024.03.22 [TypeScript]Literal Types, Readonly, const 정리 (0) 2024.03.19 [TypeScript] TypeAlias ? (0) 2024.03.19 [TypeScript] Narrowing & Assertion 문법 정리 (0) 2024.03.18