μ›Ή/JavaScript

[Javascript] μƒμ„±μž ν•¨μˆ˜, ν”„λ‘œν† νƒ€μž…, prototype

Judith Hopps 2023. 1. 11. 11:43
λ°˜μ‘ν˜•

πŸ’‘ μƒμ„±μž ν•¨μˆ˜μ˜ ν•„μš”μ„±

// μ–„μ½”μΉ˜ν‚¨μ˜ 체인점을 λ‚˜νƒ€λ‚΄λŠ” 객체듀
​
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 μ—†μ΄λŠ” 였λ₯˜κ°€ λ°œμƒν•˜λ―€λ‘œ ν•„μš”μ—†μŒ
λ°˜μ‘ν˜•