일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- javascript
- 논리연산자
- null
- 산술연산자
- Number
- literaltype
- array
- Narrowing
- elseif
- mouseout
- click
- typescript
- 타입스크립트
- Class
- function
- jQuery
- Chaining
- html
- methods
- TAB
- 비교연산자
- 증감연산자
- undefined
- 제어문
- EVENT
- type
- if
- slider
- Object
- boolean
- Today
- Total
angsu
[코딩애플] 11.Type Script 본문
class 만들 때 타입지정 가능
🍎 필드값 타입지정
class Person {
data:number = 0; // 타입 지정
}
let john = new Person();
let kim = new Person();
console.log(john.data);
console.log(kim.data);
🍎 constructor 타입지정
class Person {
constructor (){
this.name = 'kim';
this.age = 20;
}
} // Error : Property 'name' does not exist on type 'Person'
해결책.
class Person {
name;
age;
// 필드값 지정해야 오류가 안남
constructor (){
this.name = 'kim';
this.age = 20;
}
}
constructor 함수에 변수 넣기
class Person {
name;
age;
constructor (a){
this.name = a;
this.age = 20;
}
}
Q. 타입지정은 어떻게하게요
name 속성에는 string만 들어올 수 있게 타입지정 해보십시오.
참고로 constructor 함수는 return 타입지정을 하면 안됩니다.
constructor에 의해서 항상 object자료가 생산되기 때문에 생각해보면 의미없습니다.
답.
class Person {
name;
age;
constructor (a :string){
this.name = a;
this.age = 20;
}
}
Q. 필드값이랑 constructor랑 똑같은 역할이네요? 왜 구분해놓음?
들켰군요 똑같은 기능을 합니다.
근데 new Person() 사용할 때 파라미터로 뭔가 집어넣고 싶으면 constructor로 만들어야합니다.
🍎 methods 타입지정
class Person {
add(숫자){
console.log(숫자 + 1)
}
}
🍏 (숙제1) Car 클래스를 만들고 싶습니다.
1. 대충 { model : '소나타', price : 3000 } 이렇게 생긴 object를 복사해주는 class를 만들어보십시오.
2. 그리고 복사된 object 자료들은 .tax() 라는 함수를 사용가능한데 현재 object에 저장된 price의 10분의1을 출력해주어야합니다.
3. model과 price 속성의 타입지정도 알아서 잘 해보십시오. tax() 함수의 return 타입도요.
(동작 예시)
let car1 = new Car('소나타', 3000)
console.log(car1) //콘솔창 출력결과는 { model : '소나타', price : 3000 }
console.log(car1.tax()) //콘솔창 출력결과는 300
답.
class Car {
model;
price;
tax;
constructor (a :string, b :numder){
this.model = a;
this.price = b;
this.tax = 300;
}
}
let car1 = new Car('소나타', 3000)
console.log(car1) //콘솔창 출력결과는 { model : '소나타', price : 3000 }
console.log(car1.tax()) //콘솔창 출력결과는 300
/*-------------------------- 오답정리 --------------------------*/
class Car {
model :string;
price :number;
constructor (a :string, b :numder){
this.model = a;
this.price = b;
}
}
tax() :number{
return this.price * 0.1
}
let car1 = new Car('소나타', 3000)
console.log(car1) //콘솔창 출력결과는 { model : '소나타', price : 3000 }
console.log(car1.tax()) //콘솔창 출력결과는 300
🍏 (숙제2) class인데 파라미터가 잔뜩 들어가는 class Word를 만들어봅시다.
1. object 만들 때 new Word() 소괄호 안에 숫자 혹은 문자를 입력하면
2. 숫자는 전부 object 안의 num 속성 안에 array 형태로 저장되고
3. 문자는 전부 object 안의 str 속성 안에 array 형태로 저장되는 class를 만들어봅시다.
4. class 만들 때 넣을 수 있는 숫자와 문자 갯수는 일단 제한은 없습니다. 그리고 타입 빼먹지 마셈
(동작 예시)
let obj = new Word('kim', 3, 5, 'park');
console.log(obj.num) //[3,5]
console.log(obj.str) //['kim', 'park']
답.
class Word {
num :number[];
str :string[];
constructor(a :number[], b :string[]){
this.num = a;
this.str = b;
}
}
let obj = new Word('kim', 3, 5, 'park');
console.log(obj.num) //[3,5]
console.log(obj.str) //['kim', 'park']
/*-------------------------- 오답정리 --------------------------*/
class Word {
num;
str;
constructor(...param : (number | string)[]){
let 숫자들 :number[] = [];
let 문자들 :string[] = [];
param.forEach((i)=>{
if (typeof i === 'string'){
문자들.push(i)
} else {
숫자들.push(i)
}
})
this.num = a;
this.str = b;
}
}
let obj = new Word('kim', 3, 5, 'park');
console.log(obj.num) //[3,5]
console.log(obj.str) //['kim', 'park']
참고 사이트 : https://codingapple.com
'IT > Type Script' 카테고리의 다른 글
[코딩애플] 13.Type Script (0) | 2024.04.08 |
---|---|
[코딩애플] 12.Type Script (0) | 2024.04.07 |
[코딩애플] 10.Type Script (0) | 2024.04.03 |
[코딩애플] 9.Type Script (0) | 2024.04.01 |
[코딩애플] 8.Type Script (0) | 2024.04.01 |