본문 바로가기

Kakao Cloud School/카카오클라우드스쿨 수업

[Javascript] 객체(2)

객체는 Property의 집합


property는 property attribute(속성)를 가진다.

Javascript 객체는 내부 slot과 내부 method를 가지고 있다. ( 이것도 역시 property라고 할 수 있다 )

내부 slot과 내부 method는 개발자가 직접적으로 사용할 수 없다. ( Javascript Engine에 의해 사용이 가능하다 )

 

내부 slot [ [ ... ] ]
내부 method [ [ ... ] ]

 

obj.__poroto__ 를 사용하면 직접적으로 내부 slot에 접근할 수 있다.

obj.__poroto__ 는 [[Prototype]]에 접근할 수 있다.

property attribute

property를 생성할 때 해당 property의 상세를 나타내는 값이다. (기본적으로 정의된다)

property의 상세

1. property의 값 [[Value]] 

2. property 값을 수정할 수 있는지 여부 [[Writable]]

3. 해당 property가 열거될 수 있는지 여부 (for문 등을 이용해서 반복 가능한지) [[Enumerable]]

4. property attribute를 재정의할 수 있는지 여부 [[Configurable]]

descripter 객체의 property 가져오기

// Property Attribute를 확인해보자
const person = {
    name: 'Lee',
    age: 20
};

// descripter 객체의 property를 가져옴
console.log(Object.getOwnPropertyDescriptor(person, 'name'));
// { value: 'Lee', writable: true, enumerable: true, configurable: true }

// 전체 property를 가져옴
console.log(Object.getOwnPropertyDescriptors(person));
/*
{
  name: {
    value: 'Lee',
    writable: true,
    enumerable: true,
    configurable: true
  },
  age: { value: 20, writable: true, enumerable: true, configurable: true }
}
*/

Property define(정의)

Object.defineProperty( )

const person = {
    age: 20
};

// person.name = '홍길동';
Object.defineProperty(person, 'name', {
    value: '홍길동',
    writable: false,
    enumerable:false,
    configurable: true
});

console.log(person);
console.log(Object.getOwnPropertyDescriptor(person, 'name'));

// writable =  false
person.name = '아이유';
console.log(person); // 홍길동 (아이유로 바뀌지 않음, writable이 false임)

// enumerable = false
console.log(Object.keys(person)); // enumerable을 false로 주었기 때문에 나올 수 없다

// for-in 구문을 이용해 key 구하기
for(let idx in person){
    console.log(idx); // property key
}

 

 

'Kakao Cloud School > 카카오클라우드스쿨 수업' 카테고리의 다른 글

[Javascript] 데이터 타입  (0) 2022.07.05
[Javascript] History of Javascript, ES6  (0) 2022.07.05
[Javascript] 스코프  (0) 2022.07.05
[Javascript] 함수  (0) 2022.07.05
[Javascript] 객체  (0) 2022.07.05