π‘ μμ±μ ν¨μμ νμμ±
// μμ½μΉν¨μ 체μΈμ μ λνλ΄λ κ°μ²΄λ€
β
const chain1 = {
name: 'νκ΅',
no: 3,
introduce () {
return `μλ
νμΈμ, ${this.no}νΈ ${this.name}μ μ
λλ€!`;
}
};
β
const chain2 = {
name: 'κ°λ¨',
no: 17,
introduce () {
return `μλ
νμΈμ, ${this.no}νΈ ${this.name}μ μ
λλ€!`;
}
};
β
const chain3 = {
name: 'μ μ£Ό',
no: 24,
introduce () {
return `μλ
νμΈμ, ${this.no}νΈ ${this.name}μ μ
λλ€!`;
}
};
β
// μ΄μ²λΌ κ°μ νμμ κ°μ²΄λ€μ λ€μ λ§λ€μ΄μΌ νλ€λ©΄?
I. μμ±μ ν¨μλ‘ κ°μ²΄ λ§λ€κΈ°
// μμ±μ ν¨μ
function YalcoChicken (name, no) {
this.name = name;
this.no = no;
this.introduce = function () {
return `μλ
νμΈμ, ${this.no}νΈ ${this.name}μ μ
λλ€!`;
}
}
// μΈμ€ν΄μ€ μμ±
const chain1 = new YalcoChicken('νκ΅', 3);
const chain2 = new YalcoChicken('κ°λ¨', 17);
const chain3 = new YalcoChicken('μ μ£Ό', 24);
console.log(chain1, chain1.introduce());
console.log(chain2, chain2.introduce());
console.log(chain3, chain3.introduce());
- μμ±μ ν¨μλͺ
μ μΌλ°μ μΌλ‘ λλ¬Έμλ‘ μμ - νμ€μΉΌ μΌμ΄μ€
- μμ±μ ν¨μλ‘ λ§λ€μ΄μ§ κ°μ²΄λ₯Ό μΈμ€ν΄μ€ instance λΌ λΆλ¦
- this.~λ‘ μμ±λ μΈμ€ν΄μ€μ νλ‘νΌν°λ€ μ μ
- μμ±μ ν¨μλ new μ°μ°μμ ν¨κ» μ¬μ©
- μ묡μ μΌλ‘ this λ°ν
- μμ±μ ν¨μμμλ λ©μλ μ μ λΆκ° - κ°μ²΄ 리ν°λ΄κ³Ό ν΄λμ€μμλ κ°λ₯
β οΈ newλ₯Ό λΆμ΄μ§ μμΌλ©΄ undefined λ°ν
function YalcoChicken (name, no) {
this.name = name;
this.no = no;
this.introduce = function () {
return `μλ
νμΈμ, ${this.no}νΈ ${this.name}μ μ
λλ€!`;
}
}
β
console.log(YalcoChicken('νλ', 30));
- νΈμΆμ newλ₯Ό λΆμ΄λκ° μ¬λΆμ λ°λΌ νΈμΆ μλ¦¬κ° λ€λ¦
π‘ "κ°μ²΄λ₯Ό λ°ννλ ν¨μλμ λκ° λ€λ₯΄μ§??"
function createYalcoChicken (name, no) {
return {
name, no,
introduce () {
return `μλ
νμΈμ, ${this.no}νΈ ${this.name}μ μ
λλ€!`;
}
}
}
β
const chain1 = createYalcoChicken('νκ΅', 3);
const chain2 = createYalcoChicken('κ°λ¨', 17);
const chain3 = createYalcoChicken('μ μ£Ό', 24);
β
console.log(chain1, chain1.introduce());
console.log(chain2, chain2.introduce());
console.log(chain3, chain3.introduce());
II. μμ±μ ν¨μλ‘ λ§λ€μ΄μ§ κ°μ²΄
1. νλ‘ν νμ
prototype - μλ°μ€ν¬λ¦½νΈ κ°μ²΄μ§ν₯μ μ€μ¬
function YalcoChicken (name, no) {
this.name = name;
this.no = no;
this.introduce = function () {
return `μλ
νμΈμ, ${this.no}νΈ ${this.name}μ μ
λλ€!`;
}
}
β
const chain1 = new YalcoChicken('νκ΅', 3);
console.log(chain1);
// λ³Έμ¬μμ μ μ
무λ₯Ό μΆκ°
// νλ‘ν νμ
: λ³Έμ¬μμ λ°°ν¬νλ λ©λ΄μΌμ΄λΌκ³ μ΄ν΄
YalcoChicken.prototype.introEng = function () {
return `Welcome to Yalco Chicken at ${this.name}!`;
};
console.log(chain1.introEng());
console.log(new YalcoChicken('κ°λ¨', 17).introEng());
- ν μΈμ΄μ ν΄λμ€μλ λ€λ₯΄λ©° μ¬μ©νκΈ°μ λ°λΌ λ κ°λ ₯ν¨
- β οΈ μ¬μ€ introduceμ introEngμ μ’
λ₯κ° λ€λ¦ (μΈμ€ν΄μ€ vs νλ‘ν νμ
)
- μ΄ν νλ‘ν νμ
μΉμ
μμ μμΈν λ°°μ°κ² λ κ²
2. π‘ ν λ°©μμΌλ‘ λ§λ κ°μ²΄μμ μ°¨μ΄
function YalcoChicken (name, no) {
this.name = name;
this.no = no;
this.introduce = function () {
return `μλ
νμΈμ, ${this.no}νΈ ${this.name}μ μ
λλ€!`;
}
}
β
function createYalcoChicken (name, no) {
return {
name, no,
introduce () {
return `μλ
νμΈμ, ${this.no}νΈ ${this.name}μ μ
λλ€!`;
}
}
}
// κ°μ²΄ 리ν°λ΄
const chain1 = {
name: 'νκ΅', no: 3,
introduce: function () {
return `μλ
νμΈμ, ${this.no}νΈ ${this.name}μ μ
λλ€!`;
}
};
β
// κ°μ²΄ λ°ν ν¨μ
const chain2 = createYalcoChicken('κ°λ¨', 17);
β
// μμ±μ ν¨μ
const chain3 = new YalcoChicken('μ μ£Ό', 24);
console.log(chain1, chain1 instanceof YalcoChicken);
console.log(chain2, chain2 instanceof YalcoChicken);
console.log(chain3, chain3 instanceof YalcoChicken);
- κ°μ²΄ μ체μ λ‘κ·Έλ μμΈκ° λ€λ¦ μ μ μμ μμ±μ ν¨μλͺ
μ΄ λΆμ
- instanceof : κ°μ²΄κ° νΉμ μμ±μ ν¨μμ μν΄ λ§λ€μ΄μ‘λμ§ μ¬λΆ λ°ν
- νλ‘ν νμ
μ constructorμ 체μΈμ΄ ν΄λΉ μμ±μ ν¨μ ν¬ν¨νλμ§ μ¬λΆ
- μ½μμμ [[Prototype]] νΌμ³μ νμΈν΄λ³Ό κ²
3. μμ±μ ν¨μ μ체μ νλ‘νΌν°μ ν¨μ
function YalcoChicken (name, no) {
this.name = name;
this.no = no;
this.introduce = function () {
return `μλ
νμΈμ, ${this.no}νΈ ${this.name}μ μ
λλ€!`;
}
}
β
// λ³Έμ¬μ μ 보μ μ
무
YalcoChicken.brand = 'μμ½μΉν¨';
YalcoChicken.contact = function () {
return `${this.brand}μ
λλ€. 무μμ λμλ릴κΉμ?`;
};
β
const chain1 = new YalcoChicken('νκ΅', 3);
console.log(YalcoChicken.contact());
console.log(chain1.contact());
- λ€μ κ°μμ ν΄λμ€μ μ μ νλ‘νΌν° & λ©μλλ‘...
4. π‘ new μλ΅ μ€μ λ°©μ§νκΈ°
function YalcoChicken (name, no) {
this.name = name;
this.no = no;
this.introduce = function () {
return `μλ
νμΈμ, ${this.no}νΈ ${this.name}μ μ
λλ€!`;
}
β
if (!new.target) { // μ΄ λΆλΆμ μ§μ°κ³ λ€μ ν΄ λ³Ό κ²
return new YalcoChicken(name, no);
}
}
β
const chain1 = new YalcoChicken('νκ΅', 3);
const chain2 = YalcoChicken('κ°λ¨', 17);
β
console.log(chain1, chain2);
- ν΄λΉ ν¨μκ° new μ°μ°μ μμ΄ νΈμΆλμμ κ²½μ° μ¬κ·νΈμΆμ ν΅ν΄ μμ±ν΄ λ΄λ³΄λ
- λ€μ κ°μ λ°°μΈ ν΄λμ€μμλ new μμ΄λ μ€λ₯κ° λ°μνλ―λ‘ νμμμ