Design Pattern

Constructor Pattern

eddie0329 2020. 9. 24. 18:18
๋ฐ˜์‘ํ˜•

๐Ÿ“Œ ๋ชฉ์ฐจ

  • Object Creation
  • Constructor in es5 fucntion
  • Constructor in prototype
  • Constructor in Class

๐Ÿ“Œ Object Creation

๋นˆ Object๋ฅผ ์ƒ์„ฑํ•˜๋Š” 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•

// 1.
const object1 = {};
// 2.
const object2 = Object.create(Object.prototype);
// 3.
const object3 = new Object();

Object์— key์™€ value๋ฅผ ํ• ๋‹นํ•˜๋Š” 4๊ฐ€์ง€ ๋ฐฉ๋ฒ•

  • Dot Syntax

    newObject.key = "value";
  • Square Syntax

    newObject["key"] = "value";
  • Object.defineProperty Syntax

    Object.defineProperty(newObject, "key", {
      value: "value",
        writable: true,
        enumerable: true,
        configuarable: true,
    });
  • Object.defineProperties Syntax

    Object.defineProperties(newObject, {
      key: {
          value: "value",
            writable: true,
            enumerable: true,
            configuarable: true,
      }
    });

๐Ÿšจ์—ฌ๊ธฐ์„œ ์ž ๊น!

writable, enumerable, configuarable์€ ๋ฌด์—‡์ผ๊นŒ์š”?

writable: true์ผ ๊ฒฝ์šฐ ์ด ์†์„ฑ์— ์„ค์ „๋œ ๊ฐ’์„ ํ• ๋‹น ์—ฐ์‚ฐ์ž๋กœ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
enumerable: true์ผ ๊ฒฝ์šฐ ํ•ด๋‹น ๊ฐ์ฒด์˜ ์†์„ฑ์„ ์—ด๊ฑฐํ•  ๋•Œ ์ด ์†์„ฑ์ด ์—ด๊ฑฐ๋œ๋‹ค.
configurable: true์ผ ๊ฒฝ์šฐ ์ด ์†์„ฑ ์„œ์ˆ ์ž์˜ ํ˜•ํƒœ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜, ์†์„ฑ์„ ํ•ด๋‹น ๊ฐ์ฒด์—์„œ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค.

// newObject์˜ ํ˜•ํƒœ
const newObject = { key: "value" };

// enumberable์ด false ์ผ๋•Œ:
console.log(newObject); // output: {}

// writable์ด false ์ผ๋•Œ:
newObject.key = "hello";
console.log(newObject); // output: { key: "value" }

// configuarable์ด false ์ผ๋•Œ:
delete newObject.key;
console.log(newObject); // output: { key: "value" }

Object ์ƒ์†

// function that made object inherit
const defineProp = (obj, key, value) => {
    const config = {
        value,
          writable: true,
        enumerable: true,
        configurable: true,
    };
      Object.defineProperty(obj, key, config);
}

const person = Object.create(Object.prototype);
defineProp(person, "car", "BMW");
defineProp(person, "name", "Eddie");
defineProp(person, "age", 20);

console.log(person); // { car: 'BMW', name: 'Eddie', age: 20 }

const driver = Object.create(person);
defineProp(driver, "topSpeed", "100mh");
console.log(driver); // { topSpeed: '100mh' }
console.log(driver.car); // BMW

delete person.car;
console.log(person); // { name: 'Eddie', age: 20 }
console.log(driver.car); // undefined

๐Ÿ“Œ Constructor in es5 function

es5์—์„œ๋Š” class๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์•˜๊ธฐ๋•Œ๋ฌธ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑ์„ ํ•˜์˜€์—ˆ์Šต๋‹ˆ๋‹ค.

// es5
function Car(model, year, miles) {
  this.model = model;
  this.year = year;
  this.miles = miles;

  this.toString = function () {
    return `${this.model} has done ${this.miles} miles.`;
  };
}

const car = new Car("BMW", 2013, 1000);
console.log(car.toString()); // BMW has done 1000 miles.

ํ•˜์ง€๋งŒ ์ด๋ฐฉ๋ฒ•์€ toString()์ด ๋งค๋ฒˆ ์ƒˆ๋กญ๊ฒŒ ์ •์˜๊ฐ€ ๋œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ prototype์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๊ฒฐ์„ ํ• ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ“Œ Constructor in prototype

function Car(model, year, miles) {
  this.model = model;
  this.year = year;
  this.miles = miles;
}

Car.prototype.toString = function () {
  return `${this.model} has done ${this.miles} miles`;
};

const car = new Car("BMW", 2013, 10000);
console.log(car.toString()); // BMW has done 10000 miles

๐Ÿ“Œ Constructor in Class

es6์—์„œ class๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”?

class Car {
  constructor(model, year, miles) {
    this.model = model;
    this.year = year;
    this.miles = miles;
  }

  toString() {
    return `${this.model} has done ${this.miles} miles.`;
  }
}

const car = new Car("BMW", 2013, 1000);
console.log(car.toString()); // BMW has done 1000 miles.
๋ฐ˜์‘ํ˜•