๐ TypeScript/๊ฐ๋
[์ฝ๋ฉ์ ํ] ํ์ ์คํฌ๋ฆฝํธ์์์ class : constructor, prototype ํ์ ์ง์
Zoeeey
2023. 12. 20. 19:17
ํ์ ์คํฌ๋ฆฝํธ์์ class์ constructor ์ฌ์ฉํ๊ธฐ
์ผ๋ฐ์ ์ธ JavaScript ํด๋์ค ๋ฐ ๊ฐ์ฒด ์์ฑ์ ์์์ด๋ค.
// JavaScript
class Candidate {
constructor(personality, age, gender) {
this.personality = personality;
this.age = age;
this.gender = gender;
}
}
const candidate1 = new Candidate('good', 28, 'woman');
JavaScript์์๋ ํ์ ์ ๋ณด๋ฅผ ๋ช ์์ ์ผ๋ก ์ ์ธํ์ง ์๋๋ค. ๊ทธ๋ ๊ฒ ๋๋ฌธ์ ์์ฒ๋ผ๋ง ํ๊ธฐํด๋ ๋์ง๋ง, ํ์ ์คํฌ๋ฆฝํธ์์๋ ๊ฐ ์์ฑ๊ณผ ๋งค๊ฐ๋ณ์์ ํ์ ์ง์ ์ ํด์ฃผ์ด์ผ ํ๋ค.
// TypeScript
class Candidate {
personality: string;
age: number;
gender: string;
constructor(personality: string, age: number, gender: string) {
this.personality = personality;
this.age = age;
this.gender = gender;
}
}
const candidate1 = new Candidate('good', 28, 'woman');
constructor์๋ ๋ณต์ ๋๋ ๊ฒ์ด ํญ์ object์ด๊ธฐ ๋๋ฌธ์ return ํ์ ์ ์ง์ ํ ํ์๋ ์๋ค.
ํ์ ์คํฌ๋ฆฝํธ์์ class prototype์ methods ํจ์ ๋ง๋ค๊ธฐ
class Candidate {
personality: string;
age: number;
gender: string;
constructor(personality: string, age: number, gender: string) {
this.personality = personality;
this.age = age;
this.gender = gender;
}
selectCadidate(message :string){
console.log(message)
}
}
const candidate1 = new Candidate('good', 28, 'woman');
candidate1.selectCadidate('์๋
ํ์ธ์?');
ํ๋ผ๋ฏธํฐ & return ํ์ ์ง์ ์์ ๋กญ๊ฒ ํ ์ ์๋ค.
์ถ์ฒ : ์ฝ๋ฉ์ ํ https://codingapple.com/ / TypeScript Documentation https://www.typescriptlang.org/docs/handbook/2/objects.html