interface Person4 {
name: string;
age: number;
hello(): void;
}
const p41: Person4 = {
name: 'malza',
age: 29,
// 함수를 만드는 방법은 여러가지다.
hello: function (): void {
console.log(`내 이름은 ${this.name} 이라고 해!`);
},
};
const p42: Person4 = {
name: 'Amy',
age: 20,
// this 를 매개변수에 넣어줄수도 있다.
// TS 의 함수에서는 (constructor 제외!)첫번째 인자를 this 로 넣어줄수 있다.
hello(this: Person4): void {
console.log(`내 이름은 ${this.name} 이라고 해!`);
},
};
// const p43: Person4 = {
// name: 'Tom',
// age: 29,
// hello: (): void => {
// console.log(`내 이름은 ${this.name} 이라고 해!`); //error! 화살표 함수에서의 this 불가능
// },
// };
p41.hello(); // 잘 나온다.
p42.hello(); // 잘 나온다.