Object() constructor
new Object(), new Object(undefined), new Object(null) 은 빈 객체를 반환한다 // {}
Object.assign()
Object.assign(target, source); target에게 source를 덮어쓴다.
const obj = { a: 1 };
const copy = Object.assign({}, obj);
console.log(copy); // { a: 1 }
const o1 = { a: 1 };
const o2 = { b: 2 };
const o3 = { c: 3 };
const obj = Object.assign(o1, o2, o3);
console.log(obj); // { a: 1, b: 2, c: 3 }
console.log(o1); // { a: 1, b: 2, c: 3 }, 목표 객체 자체가 변경됨.
o1.d=4 // 이렇게 하면 obj와 o1이 같은 결과를 출력함.
마지막 에 o1이 target이 되면서 o1에 갚이 할당됨.
** 주의할점은 Deep Copy는 되지 않으므로 Deep Copy를 위해서는 JSON.parse(JSON.stringify(target))해야 한다.
** 같은 속성을 가진다면 병합할 때 덮어써짐
*** enumerable: true인 것만 복사 가능!!
자바스크립트 객체에서 속성은 이름과 descriptor로 구성된다
Object.entries가 지원하지 않을 때...
if (!Object.entries)
Object.entries = function (obj) {
var ownProps = Object.keys(obj),
i = ownProps.length,
resArray = new Array(i); // preallocate the Array
while (i--) resArray[i] = [ownProps[i], obj[ownProps[i]]];
return resArray;
};
와.................
Object.entries를 Map으로 변환
const obj = { foo: "bar", baz: 42 };
const map = new Map(Object.entries(obj));
console.log(map); // Map { foo: "bar", baz: 42 }
const obj = { foo: "bar", baz: 42 };
Object.entries(obj).forEach(([key, value]) => console.log(`${key}: ${value}`)); // "foo: bar", "baz: 42"
value: 속성 값(데이터 descriptor만 존재)
writable: 쓰기 속성
configurable: 유형 변경여부 및 삭제 가능 여부
enumerable: 열거형으로 사용할 수있는지 여부 { name, age } 등?
var o, d;
o = {
get foo() {
return 17;
},
};
d = Object.getOwnPropertyDescriptor(o, "foo");
// d는 { configurable: true, enumerable: true, get: /* getter 함수 */, set: undefined }
반응형
'Nodejs' 카테고리의 다른 글
TypeScript tsconfig-paths 톺아보기 (0) | 2023.07.19 |
---|---|
Typescript tsconfig.json 옵션들 톺아보기 (0) | 2023.07.19 |
Node.js 백엔드 디렉토리 구조 (0) | 2023.07.19 |
async / await 와 Promise (async 안에서 promise 처리) (0) | 2018.06.29 |
[NODEJS 입문]14.(pbkdf2-password) 비밀번호 암호화 (0) | 2018.06.12 |