νμ μ€ν¬λ¦½νΈμμ 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