// dict의 key-value 한 쌍이 object의 property
// method: obejct 안에 정의된 함수
// factory function: 동일한 property와 매소드를 지닌 object(객체)를 균일하게 생성하는 함수 (클래스보다 가볍게 생성할 때 사용)
const robotFactory1 = (model, mobile) => {
return {
model: model,
mobile: mobile,
beep() {
console.log('Beep Boop');
}
};
};
const tinCan = robotFactory1('P-500', true);
tinCan.beep();
// property value shorthand (destructuring technique): key와 value 이름이 같을 때 아래처럼 축약 가능
const robotFactory = (model, mobile) => ({
model,
mobile,
beep() {
console.log('Beep Boop');
}
});
const newRobot = robotFactory('P-501', false)
console.log(newRobot.model)
console.log(newRobot.mobile)
// Destructured Assignment: 특정 properties를 바로 local 변수로 만들어 쓰기 위함 (깊이도 줄이고 가독성 향상)
const robot = {
model: '1E78V2',
energyLevel: 100,
functionality: {
beep() {
console.log('Beep Boop');
},
fireLaser() {
console.log('Pew Pew');
},
}
};
const {functionality} = robot;
console.log(functionality);
functionality.beep();
// 기본 내장된 object 관련 함수들
const robot3 = {
model: 'SAL-1000',
mobile: true,
sentient: false,
armor: 'Steel-plated',
energyLevel: 75
};
// key를 list[str]로 호출하는 함수, 대상을 ()안에 넣는다.
const robotKeys = Object.keys(robot3);
console.log(robotKeys);
// list[list[str]]형태로 key, value만 담은 리스트 나열
const robotEntries = Object.entries(robot3);
console.log(robotEntries);
// Object.assign(추가할 {key:value, ...}, 대상 object) 수행시 {}형태로 쌍맞춰 내용 추가해서 출력
const newRobot3 = Object.assign({laserBlaster: true, voiceRecognition: true}, robot);
console.log(newRobot3);728x90
반응형
'Programming > JavaScript' 카테고리의 다른 글
| Promise 추가 내용 (0) | 2026.05.13 |
|---|---|
| this, arrow fucntion, template literals, tagged literals, spread operator, rest 파라미터, apply, call, Class, extends (1) | 2026.05.12 |
| [CodeAcademy] object, this, getter, setter (1) | 2026.04.08 |
| [CodeAcademy] Array, loop, Iterators (0) | 2026.04.08 |
| [CodeAcademy] 함수 (0) | 2026.04.08 |