문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 양쪽 이전 판이전 판다음 판 | 이전 판 | ||
| typescript:class [2019/02/01 01:13] – [TypeScript Class] taekgu | typescript:class [2025/04/15 10:05] (현재) – 바깥 편집 127.0.0.1 | ||
|---|---|---|---|
| 줄 41: | 줄 41: | ||
| 위의 코드는 Java에서 우리가 익히 보아왔던 class의 형태입니다. 생성자를 표현하는 부분이 좀 생소하지만 쉽게 이해할 수 있을 듯 합니다. | 위의 코드는 Java에서 우리가 익히 보아왔던 class의 형태입니다. 생성자를 표현하는 부분이 좀 생소하지만 쉽게 이해할 수 있을 듯 합니다. | ||
| ==== constructor ==== | ==== constructor ==== | ||
| + | Typescript의 Class에서 constructor는 특별하게 보아야 한다. constructor의 파라메터에서 class의 property로 선언된다.(물론 new연산자의 파라메터를 받는다.) | ||
| <code javascript> | <code javascript> | ||
| class Greeter { | class Greeter { | ||
| - | constructor(public greeting: string) {} | + | constructor(public greeting: string, private myService: HelloServe) {} |
| greet() { | greet() { | ||
| return " | return " | ||
| 줄 178: | 줄 179: | ||
| <code javascript> | <code javascript> | ||
| class Book { | class Book { | ||
| - | |||
| public readonly btitle: string; | public readonly btitle: string; | ||
| - | |||
| constructor(btitle: | constructor(btitle: | ||
| this.btitle = btitle; | this.btitle = btitle; | ||
| } | } | ||
| - | |||
| } | } | ||
| - | |||
| let book:Book = new Book(' | let book:Book = new Book(' | ||
| + | book.btitle = ' | ||
| - | book.btitle = ' | ||
| 다음의 예제처럼 생성자의 parameter를 readonly로 선언하면 따로 class의 property로 선언할 필요가 없습니다. | 다음의 예제처럼 생성자의 parameter를 readonly로 선언하면 따로 class의 property로 선언할 필요가 없습니다. | ||
| <code javascript> | <code javascript> | ||
| class Book { | class Book { | ||
| - | |||
| constructor(readonly btitle: string) { | constructor(readonly btitle: string) { | ||
| this.btitle = btitle; | this.btitle = btitle; | ||
| } | } | ||
| - | |||
| } | } | ||
| - | |||
| let book:Book = new Book(' | let book:Book = new Book(' | ||
| - | + | console.log(book.btitle); | |
| - | console.log(book.btitle); | + | </ |
| ==== Static Property ==== | ==== Static Property ==== | ||
| static 키워드 역시 사용할 수 있습니다. ECMAScript 2015에서는 static을 method에만 적용할 수 있었지만 TypeScript는 property에도 적용할 수 있습니다. static property는 class의 이름으로 직접 access를 할 수 있습니다. | static 키워드 역시 사용할 수 있습니다. ECMAScript 2015에서는 static을 method에만 적용할 수 있었지만 TypeScript는 property에도 적용할 수 있습니다. static property는 class의 이름으로 직접 access를 할 수 있습니다. | ||