
객체는 복합 데이터를 담은 그릇이다 .
코드 📄
let coworkers ={
programmer:"khw",
designer:"aaa",
'frontend Developer': "bbb",
['Backend Developer'] : "ccc"
};
코드 📄
console.log(coworkers.programmer); // khw
console.log(coworker['frontend Developer']); // bbb
코드 📄
coworkers.bookKeeper ="ddd";
coworkers["data scientist"] = "eee";
console.log(coworkers);
/* {
programmer: 'khw',
designer: 'aaa',
'frontend Developer': 'bbb',
'Backend Developer': 'ccc',
bookKeeper: 'ddd',
'data scientist': 'eee'
}
*/
delete 사용
코드 📄
delete coworkers.bookKeeper;
대괄호 표기법을 사용해서 접근
코드 📄
function getKey(obj,key){
return obj[key];
}
console.log(getKey(coworkers,"programmer"));
프로퍼티 값을 변수로 받았을 때 키와 값의 이름이 같을때 사용한다.
코드 📄
function makeUser(name, age){
return{
name, // name: name 같다.
age // age: age와 같다.
};
}
객체의 value에 함수가 들어갈때 메소드라고 부른다.
코드 📄
coworkers.showAll = function(){
/* 객체에 있는 key값들을 가져오는 반복문 */
for(let key in this){
console.log(key+ '='+this[key]);
}
}
coworkers.showAll();
//출력
/*
programmer=khw
designer=aaa
frontend Developer=bbb
Backend Developer=ccc
bookKeeper=ddd
data scientist=eee
showAll=function(){
// 객체에 있는 key값들을 가져오는 반복문 //
for(let key in this){
console.log(key+ '='+this[key]);
}
}
/*
생성자 함수와 new연산자를 통해 객체를 생성할 수 있다.
'new' 연산자를 붙여 실행한다.코드 📄
function Person(name){
this.name = name; // this가 가리키는 것은 Person생성자 함수가 생성한 객체
this.introduce =function(){
return 'My name is ' + this.name;
}
}
// person1,2객체가 생성되서 name속성과 introduce메소드에 값이 저장된다.
let person1 =new Person('khw');
let person2 =new Person('kar');
console.log(person1.introduce());
// My name is khw
객체의 정적 메소드는 Object라는 이름의 [[Prototype]]에 의해 상속되어 사용된다.
코드 📄
const khw ={ name:'khw',emoji:'🧑'};
객체의 키,값 , 키/값 쌍을 출력해주는 메소드
코드 📄
console.log(Object.keys(khw)); // [ 'name', 'emoji' ]
console.log(Object.values(khw)); // [ 'khw', '🧑' ]
console.log(Object.entries(khw)); // [ [ 'name', 'khw' ], [ 'emoji', '🧑' ] ]
오브젝트 각각의 프로퍼티는 프로퍼티 디스크립터라고 하는 객체로 저장된다.
주어진 객체의 속성에 대한 속성 설명자(객체)를 반환한다.
코드 📄
console.log(Object.getOwnPropertyDescriptor(khw,"name"));
//{ value: 'khw', writable: true, enumerable: true, configurable: true }
반환되는 속성 설명자에는 value, writable,enumerable,configurable 이 있다.
객체에 특정 속성에 새로운 속성을 직접 정의하거나 이미 존재하는 속성을 수정한 후, 해당 객체를 반환한다.
코드 📄
Object.defineProperty(khw,"name",{
value:'hyun00',
writable: false,
enumerable: false,
configurable: false
})
console.log(khw.name); // hyun00
console.log(Object.keys(khw)); //[ 'emoji' ] name은 열거 불가능
delete khw.name; // name 속성을 삭제할 수 없음
console.log(khw.name); // hyun00
주어진 객체의 속성 전부에 대한 설명자(객체)를 반환한다.
코드 📄
console.log(Object.getOwnPropertyDescriptors(khw));
/*
{
name: {
value: 'khw',
writable: true,
enumerable: true,
configurable: true
},
emoji: { value: '🧑', writable: true, enumerable: true, configurable: true }
}
객체에 모든 속성에 새로운 속성을 정의하거나 기존의 속성을 수정하고, 그 객체를 반환한다.
코드 📄
Object.defineProperties(khw,{
name:{
value:'hyun00',
writable: false,
enumerable: false,
configurable: false
},
emoji:{
value:'👲',
writable: false,
enumerable: false,
configurable: false
}
})
target 객체에 sources 객체의 속성들을 복사하고 복사된 객체를 반환한다.
코드 📄
const khw ={name:"khw",age:25};
const kar ={};
Object.assign(kar,khw);
console.log(khw); // { name: 'khw', age: 25 }
console.log(kar); // { name: 'khw', age: 25 }
코드 📄
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 }
객체를 동결한다. 동결된 객체는 더 이상 변경되지 않는다.
즉 객체의 속성 추가, 삭제, 수정, 재정의는 모두 불가하다.
단 freeze할 객체 안에 내부 객체가 있다면 내부객체까지는 freeze되지 않는다.
왜냐하면 객체는 shallow freeze 되기 때문이다.
코드 📄
const obj1 ={a:1};
const obj2 ={b: 2,obj1};
Object.freeze(obj2);
console.log(obj2); // { b: 2, obj1: { a: 1 } }
obj2.c = 3; // x
delete obj2.b; // x
obj2.b =4; // x
obj1.a= 4; // o
//obj2를 freeze하여도 obj1까지는 freeze되지 않는다.(얕은 얼림)
console.log(obj2); // { b: 2, obj1: { a: 4 } }
console.log(Object.isFrozen(obj2)); // true
// isFrozen(obj) 메소드는 obj가 freeze된 상태인지 boolean값 출력
seal 메소드는 객체를 밀봉한다.
즉 객체의 속성 추가, 삭제, 재정의는 모두 불가하지만 수정은 가능하다.
코드 📄
const obj1 ={a:1};
const obj2 ={b: 2,obj1};
const obj3 ={...obj2};
Object.seal(obj3);
console.log(obj3);
obj3.b =3; // 수정 o
delete obj3.b; // 삭제 x
obj3.c = 4; // 추가 x
console.log(obj3); // { b: 2, obj1: { a: 1 } }
console.log(Object.isSealed(obj3)); // true
// isSealed(obj) 메소드는 obj가 seal된 상태인지 boolean값 출력
새로운 속성이 객체에 추가되는 것을 방지한다.
코드 📄
const dog ={sound: '왈왈'};
Object.preventExtensions(dog);
dog.size= 'small'; // x
console.log(dog); //{ sound: '왈왈' }
console.log(Object.isExtensible(dog)); // false
// isExtensible(obj) 메소드는 obj가 확장 가능한지 boolean값 출력