https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Object
Object 객체에 정의된 메소드들 중 프로토타입으로 정의되지 않은 메소드들을 정적 메소드라고 한다.
이들은
(1) Object.assign()
열거할 수 있는 하나 이상의 출처 객체로부터 대상 객체로 속성을 복사할 때 사용한다.
ex. Object.assign(target, source)
target 객체에 source 객체를 병합한다.
이 때 출처 객체로는 둘 이상의 객체가 들어갈 수 있다.
ex.
const a = {
name: 'honey',
age: 20
}
const b = {
department:'computer science'
}
const c = {
grade: 4.0
}
const target = Object.assign(a,b,c)
console.log(a)
그리고 target은 a와 완전히 같은 메모리 주소를 가지고 있는 같은 객체이다.
실재로 target === a
연산은 true 를 리턴한다.
이 때 원본 객체를 훼손하지 않고 아예 새로운 객체를 얻으려면?
ans.
const a = {
name: 'honey',
age: 20
}
const b = {
department:'computer science'
}
const c = {
grade: 4.0
}
const target = Object.assign({},a,b,c)
console.log(target)
이렇게 하면 target은 assign 맨 앞의 매개변수로 쓰인 새로 만든 객체와 주소를 공유하므로 a와 같은 객체가 아니게 된다.
(2) Object.keys()
객체의 key들을 항목으로 하는 배열을 만들어 리턴하는 메소드
ex.
const user = {
name: 'table',
age: 50,
email: 'hashtable@naver.com'
}
const keys = Object.keys(user)
console.log(keys)
console.log(user['email'])
console.log(user.email)
const values = keys.map(key => user[key])
console.log(values)
결과
결과를 보면 key들은 모두 문자열 데이터로 저장이 되어 있다.
그리고 user['eamil'] 문법을 보았을 때
[]를 이용하여 객체의 value를 호출하려면 문자열을 이용해야 한다는 것을 알 수 있다.