9. 객체

Veloger_97Β·2021λ…„ 5μ›” 9일
0
post-thumbnail

9.1 객체 μƒμ„±ν•˜κΈ°πŸ‘Š


9.1.1 객체의 μƒμ„±μž

μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ κ°μ²΄λŠ” 이름과 값을 ν•œ 쌍으둜 묢은 집합이닀. ν•œ μŒμ„ 이룬 것을 ν”„λ‘œνΌν‹° κ·Έκ²ƒμ˜ 이름을 ν‚€(key)라고 ν•œλ‹€. κ°’μœΌλ‘œλŠ” λͺ¨λ“  데이터 νƒ€μž…μ˜ 데이터λ₯Ό μ €μž₯ν•  수 μžˆλ‹€.
객체λ₯Ό μƒμ„±ν•˜λŠ” λ°©λ²•μ—λŠ” λ‹€μŒ 세가지가 μžˆλ‹€.

1. 객체 λ¦¬ν„°λŸ΄λ‘œ μƒμ„±ν•˜λŠ” 방법

var card = { suit: "ν•˜νŠΈ", rank: "A" };

2. μƒμ„±μžλ‘œ μƒμ„±ν•˜λŠ” 방법

function Card(suit, rank) {
  this.suit = suit;
  this.rank = rank;
}
var card = new Card("ν•˜νŠΈ", "A");
console.log(card); // Card { suit: 'ν•˜νŠΈ', rank: 'A' }

3. Object.create둜 μƒμ„±ν•˜λŠ” 방법

var card = Object.create(Object.prototype, {
  suit: {
    value: "ν•˜νŠΈ",
    writable: true,
    enumerable: true,
    configurable: true,
  },
  rank: {
    value: "A",
    writable: true,
    enumerable: true,
    configurable: true,
  },
});
console.log(card); // Card { suit: 'ν•˜νŠΈ', rank: 'A' }

9.1.2 ν”„λ‘œν† νƒ€μž…

μƒμ„±μž μ•ˆμ—μ„œ λ©”μ„œλ“œλ₯Ό μ •μ˜ν•  λ•Œ 문제점

μƒμ„±μž μ•ˆμ—μ„œ this 뒀에 λ©”μ„œλ“œλ₯Ό μ •μ˜ν•˜λ©΄ λͺ¨λ“  μΈμŠ€ν„΄μŠ€μ— λ˜‘κ°™μ€ λ©”μ„œλ“œκ°€ μΆ”κ°€λœλ‹€. λ”°λΌμ„œ 같은 μž‘μ—…μ„ ν•˜λŠ” λ©”μ„œλ“œλ₯Ό μΈμŠ€ν„΄μŠ€ 개수만큼 μƒμ„±ν•˜κ²Œ 되며 그만큼의 λ©”λͺ¨λ¦¬λ₯Ό μ†ŒλΉ„ν•˜κ²Œ λœλ‹€.

function Circle(center, radius) {
  this.center = center;
  this.radius = radius;
  this.area = function () {
    return Math.PI * this.radius * this.radius;
  };
}

// 각각의 μΈμŠ€ν„΄μŠ€κ°€ λ˜‘κ°™μ€ λ©”μ„œλ“œ areaλ₯Ό μ†Œμœ ν•œλ‹€.
var c1 = new Circle({ x: 0, y: 0 }, 2.0);
var c2 = new Circle({ x: 0, y: 1 }, 3.0);
var c3 = new Circle({ x: 1, y: 0 }, 1.0);

μ΄λŸ¬ν•œ λ¬Έμ œλŠ” ν”„λ‘œν† νƒ€μž… 객체에 λ©”μ„œλ“œλ₯Ό μ •μ˜ν•˜λŠ” λ°©μ‹μœΌλ‘œ ν•΄κ²° ν•  수 μžˆλ‹€.

ν”„λ‘œν† νƒ€μž… 객체

JSλŠ” ν•¨μˆ˜λ„ 객체이기 λ•Œλ¬Έμ— 기본적으둜 prototype ν”„λ‘œνΌν‹°λ₯Ό κ°–κ³  μžˆλ‹€.

ν•¨μˆ˜μ˜ prototype ν”„λ‘œνΌν‹°κ°€ κ°€λ¦¬ν‚€λŠ” 객체λ₯Ό κ·Έ ν•¨μˆ˜μ˜ ν”„λ‘œν† νƒ€μž… 객체라고 ν•œλ‹€. 기본적으둜 빈 객체λ₯Ό 가리킴

function F() {}
console.log(F.prototype); // Object {}

ν”„λ‘œν† νƒ€μž… 객체의 ν”„λ‘œνΌν‹°λŠ” μƒμ„±μžλ‘œ μƒμ„±ν•œ λͺ¨λ“  μΈμŠ€ν„΄μŠ€μ—μ„œ κ·Έ μΈμŠ€ν„΄μŠ€μ˜ ν”„λ‘œνΌν‹°μ²˜λŸΌ μ‚¬μš©

F.prototype.prop = "value";
var obj = new F();
console.log(obj.prop); // value

μΈμŠ€ν„΄μŠ€μ˜ ν”„λ‘œνΌν‹°μ— 값을 λŒ€μž…ν–ˆμ„ λ•Œ 이름이 같은 ν”„λ‘œνΌν‹°κ°€ 있으면 κ·Έ ν”„λ‘œνΌν‹°μ— 값을 λŒ€μž…ν•œλ‹€.

obj.prop = "instance value";
console.log(obj.prop); // instance value
console.log(F.prototype.prop); // prototype value

ν”„λ‘œν† νƒ€μž… 객체의 ν”„λ‘œνΌν‹°λ₯Ό μΈμŠ€ν„΄μŠ€μ—μ„œ μ°Έμ‘°ν•  수 μžˆλŠ” 상황을 κ°€λ¦¬μΌœ μΈμŠ€ν„΄μŠ€κ°€ ν”„λ‘œν† νƒ€μž… 객체λ₯Ό μƒμ†ν•˜κ³ μžˆλ‹€ 라고 ν•œλ‹€. μ•žμ—μ„œ μ–ΈκΈ‰ν•œ μƒμ„±μž μ•ˆμ—μ„œ this뒀에 λ©”μ„œλ“œλ₯Ό μ •μ˜ν•  λ•Œ μƒκΈ°λŠ” 문제λ₯Ό λ‹€μŒκ³Ό 같은 μ½”λ“œλ‘œ ν•΄κ²°ν•  수 μžˆλ‹€.

λͺ¨λ“  μΈμŠ€ν„΄μŠ€λŠ” area λ©”μ„œλ“œλ₯Ό μ†Œμœ ν•˜μ§€ μ•Šμ§€λ§Œ ν”„λ‘œν† νƒ€μž… 객체의 area λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•  수 μžˆλ‹€.

// Circle μƒμ„±μž
function Circle(center, radius) {
  this.center = center;
  this.radius = radius;
}
// Circle μƒμ„±μžμ˜ prototype ν”„λ‘œνΌν‹°μ— area λ©”μ„œλ“œλ₯Ό μΆ”κ°€
Circle.prototype.area = function () {
  return Math.PI * this.radius * this.radius;
};
// Circle μƒμ„±μžλ‘œ μΈμŠ€ν„΄μŠ€ 생성
var c1 = new Circle({ x: 0, y: 0 }, 2.0);
var c2 = new Circle({ x: 0, y: 1 }, 3.0);
var c3 = new Circle({ x: 1, y: 0 }, 1.0);
profile
ν”„λ‘ νŠΈμ—”λ“œ κ°œλ°œμžκ°€ 되고 μ‹Άμ–΄μš” πŸ™†β€β™‚οΈ

0개의 λŒ“κΈ€

κ΄€λ ¨ μ±„μš© 정보