๐ ๋ชฉ์ฐจ
- 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.
'Design Pattern' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Mediator Pattern (0) | 2020.10.23 |
---|---|
Observer Pattern (0) | 2020.10.19 |
Module Pattern (0) | 2020.10.07 |
Singleton Pattern (0) | 2020.10.05 |
Vue Design Patterns - Builder Director Pattern (0) | 2020.09.05 |