이런저런 IT 이야기
article thumbnail
반응형

  • Object.keys(obj): 객체의 열거 가능한 속성 이름을 배열로 반환합니다.
const obj = {
  name: 'John',
  age: 30,
  city: 'New York'
};

const keys = Object.keys(obj);

console.log(keys); // ['name', 'age', 'city']
  • Object.values(obj): 객체의 열거 가능한 속성 값들을 배열로 반환합니다.
const obj = {
  name: 'John',
  age: 30,
  city: 'New York'
};

const values = Object.values(obj);

console.log(values); // ['John', 30, 'New York']
  • Object.entries(obj): 객체의 열거 가능한 속성 이름과 값들을 [키, 값] 쌍의 배열로 반환합니다.
const obj = {
  name: 'John',
  age: 30,
  city: 'New York'
};

const entries = Object.entries(obj);

console.log(entries);
// 출력:
// [
//   ['name', 'John'],
//   ['age', 30],
//   ['city', 'New York']
// ]
  • Object.assign(target, ...sources): 하나 이상의 소스 객체의 속성을 대상 객체로 복사합니다.
const target = { a: 1, b: 2 };
const source = { b: 3, c: 4 };

const result = Object.assign(target, source);

console.log(result);
// 출력: { a: 1, b: 3, c: 4 }
console.log(target);
// 출력: { a: 1, b: 3, c: 4 }
  • Object.create(proto, [propertiesObject]): 지정된 프로토타입 객체와 속성으로 새로운 객체를 생성합니다.
const person = {
  greet() {
    console.log(`Hello, ${this.name}!`);
  }
};

const john = Object.create(person);
john.name = 'John';

john.greet(); // 출력: Hello, John!
  • Object.defineProperty(obj, prop, descriptor): 객체에 새로운 속성을 정의하거나 기존 속성을 수정합니다.
const person = {};

// 'name' 속성을 정의하고 값을 설정
Object.defineProperty(person, 'name', {
  value: 'John',
  writable: false, // 쓰기 가능 여부
  enumerable: true, // 열거 가능 여부
  configurable: true // 속성 수정 가능 여부
});

console.log(person.name); // 출력: John

// 'name' 속성의 값을 변경하려고 시도
person.name = 'Mike'; // 에러: Cannot assign to read only property 'name'

// 객체의 속성들을 열거
for (let key in person) {
  console.log(key); // 출력: name
}

// 'name' 속성을 삭제
delete person.name;

console.log(person.name); // 출력: undefined
  • Object.getOwnPropertyDescriptor(obj, prop): 객체의 속성에 대한 설명자(descriptor)를 반환합니다.
const person = {
  name: 'John',
  age: 30
};

// 'name' 속성에 대한 속성 설명자 가져오기
const nameDescriptor = Object.getOwnPropertyDescriptor(person, 'name');
console.log(nameDescriptor);
/*
출력:
{
  value: 'John',
  writable: true,
  enumerable: true,
  configurable: true
}
*/

// 'age' 속성에 대한 속성 설명자 가져오기
const ageDescriptor = Object.getOwnPropertyDescriptor(person, 'age');
console.log(ageDescriptor);
/*
출력:
{
  value: 30,
  writable: true,
  enumerable: true,
  configurable: true
}
*/
  • Object.freeze(obj): 객체를 동결(freeze)하여 수정할 수 없게 만듭니다.
const person = {
  name: 'John',
  age: 30
};

// 객체 동결
Object.freeze(person);

// 속성 값 변경 시도
person.name = 'Jane'; // 변경되지 않음
person.age = 25; // 변경되지 않음

// 새로운 속성 추가 시도
person.gender = 'male'; // 추가되지 않음

// 속성 삭제 시도
delete person.age; // 삭제되지 않음

console.log(person);
// 출력: { name: 'John', age: 30 }
  • Object.seal(obj): 객체를 밀봉(seal)하여 새로운 속성 추가를 방지하고 기존 속성을 수정할 수 없게 만듭니다.
const person = {
  name: 'John',
  age: 30
};

// 객체 밀봉
Object.seal(person);

// 속성 값 변경 시도
person.name = 'Jane'; // 변경됨
person.age = 25; // 변경됨

// 새로운 속성 추가 시도
person.gender = 'male'; // 추가되지 않음

// 속성 삭제 시도
delete person.age; // 삭제되지 않음

console.log(person);
// 출력: { name: 'Jane', age: 25 }
  • Object.isExtensible(obj): 객체가 확장 가능한지 여부를 확인합니다.
const obj1 = { name: 'John' };

console.log(Object.isExtensible(obj1));
// 출력: true

Object.preventExtensions(obj1);

console.log(Object.isExtensible(obj1));
// 출력: false
  • Object.isFrozen(obj): 객체가 동결되어 있는지 여부를 확인합니다.
const obj1 = { name: 'John' };

console.log(Object.isFrozen(obj1));
// 출력: false

Object.freeze(obj1);

console.log(Object.isFrozen(obj1));
// 출력: true
  • Object.isSealed(obj): 객체가 밀봉되어 있는지 여부를 확인합니다.
const obj1 = { name: 'John' };

console.log(Object.isSealed(obj1));
// 출력: false

Object.seal(obj1);

console.log(Object.isSealed(obj1));
// 출력: true
  • Object.getOwnPropertyNames(obj): 객체의 모든 속성 이름을 배열로 반환합니다.
const obj = {
  name: 'John',
  age: 30,
};

const propertyNames = Object.getOwnPropertyNames(obj);
console.log(propertyNames);
// 출력: [ 'name', 'age' ]
  • Object.getOwnPropertyDescriptors(obj): 객체의 모든 속성에 대한 설명자(descriptor)를 반환합니다.
const obj = {
  name: 'John',
  age: 30,
};

const descriptors = Object.getOwnPropertyDescriptors(obj);
console.log(descriptors);
/* 출력:
{
  name: {
    value: 'John',
    writable: true,
    enumerable: true,
    configurable: true
  },
  age: {
    value: 30,
    writable: true,
    enumerable: true,
    configurable: true
  }
}
*/
  • Object.getPrototypeOf(obj): 객체의 프로토타입(prototype)을 반환합니다.
const parent = {
  sayHello() {
    console.log('Hello!');
  }
};

const child = Object.create(parent);

console.log(Object.getPrototypeOf(child) === parent); // true
  • Object.hasOwnProperty(prop): 객체에 특정 속성이 직접 정의되어 있는지 확인합니다.
const person = {
  name: 'John',
  age: 30
};

console.log(person.hasOwnProperty('name')); // true
console.log(person.hasOwnProperty('age')); // true
console.log(person.hasOwnProperty('gender')); // false

console.log(person.hasOwnProperty('toString')); // false (상속된 속성)
반응형

'Javascript' 카테고리의 다른 글

Javascript Execution context  (0) 2023.05.26
JavaScript의 WebWorker  (0) 2023.05.26
JavaScript의 Math의 모든 함수들  (0) 2023.05.26
JavaScript의 배열(Array)의 모든 함수들  (0) 2023.05.25
제너레이터 함수(Generator Function)  (0) 2023.05.25
profile

이런저런 IT 이야기

@이런저런 IT 이야기

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

profile on loading

Loading...

검색 태그