현재 번역은 완벽하지 않습니다. 한국어로 문서 번역에 동참해주세요.

Object.defineProperties() 메서드는 이미 존재하거나 새로운 프로퍼티들의 각종 속성들을 재정의할 수 있습니다.

문법

Object.defineProperties(obj, props)

인자

obj
속성을 변경할 오브젝트입니다.
props
An object whose own enumerable properties constitute descriptors for the properties to be defined or modified. Property descriptors present in objects come in two main flavors: data descriptors and accessor descriptors (see Object.defineProperty() for more details). Descriptors have the following keys:
configurable
true if and only if the type of this property descriptor may be changed and if the property may be deleted from the corresponding object.
Defaults to false.
enumerable
true로 지정할 경우 해당 프로퍼티는 오브젝트를 나열하게하는 각종 함수나 메서드들의 영향을 받지 않습니다.
Defaults to false.
value
해당 프로퍼티의 키에 해당하는 값입니다. 자바스크립트에서는 숫자, 객체, 함수와 같은 다양한 값을 집어넣을 수 있습니다.
Defaults to undefined.
writable
true로 지정할 경우 해당 프로퍼티는 할당연산자를 통해 변경 가능합니다.
Defaults to false.
get
A function which serves as a getter for the property, or undefined if there is no getter. The function return will be used as the value of property.
Defaults to undefined.
set
A function which serves as a setter for the property, or undefined if there is no setter. The function will receive as only argument the new value being assigned to the property.
Defaults to undefined.

반환값

The object that was passed to the function.

Description

Object.defineProperties, in essence, defines all properties corresponding to the enumerable own properties of props on the object obj object.

예시

var obj = {};
Object.defineProperties(obj, {
  'property1': {
    value: true,
    writable: true
  },
  'property2': {
    value: 'Hello',
    writable: false
  }
  // etc. etc.
});

Polyfill

Assuming a pristine execution environment with all names and properties referring to their initial values, Object.defineProperties is almost completely equivalent (note the comment in isCallable) to the following reimplementation in JavaScript:

function defineProperties(obj, properties) {
  function convertToDescriptor(desc) {
    function hasProperty(obj, prop) {
      return Object.prototype.hasOwnProperty.call(obj, prop);
    }

    function isCallable(v) {
      // NB: modify as necessary if other values than functions are callable.
      return typeof v === 'function';
    }

    if (typeof desc !== 'object' || desc === null)
      throw new TypeError('bad desc');

    var d = {};

    if (hasProperty(desc, 'enumerable'))
      d.enumerable = !!desc.enumerable;
    if (hasProperty(desc, 'configurable'))
      d.configurable = !!desc.configurable;
    if (hasProperty(desc, 'value'))
      d.value = desc.value;
    if (hasProperty(desc, 'writable'))
      d.writable = !!desc.writable;
    if (hasProperty(desc, 'get')) {
      var g = desc.get;

      if (!isCallable(g) && typeof g !== 'undefined')
        throw new TypeError('bad get');
      d.get = g;
    }
    if (hasProperty(desc, 'set')) {
      var s = desc.set;
      if (!isCallable(s) && typeof s !== 'undefined')
        throw new TypeError('bad set');
      d.set = s;
    }

    if (('get' in d || 'set' in d) && ('value' in d || 'writable' in d))
      throw new TypeError('identity-confused descriptor');

    return d;
  }

  if (typeof obj !== 'object' || obj === null)
    throw new TypeError('bad obj');

  properties = Object(properties);

  var keys = Object.keys(properties);
  var descs = [];

  for (var i = 0; i < keys.length; i++)
    descs.push([keys[i], convertToDescriptor(properties[keys[i]])]);

  for (var i = 0; i < descs.length; i++)
    Object.defineProperty(obj, descs[i][0], descs[i][1]);

  return obj;
}

명세

Specification Status Comment
ECMAScript 5.1 (ECMA-262)
The definition of 'Object.defineProperties' in that specification.
Standard Initial definition. Implemented in JavaScript 1.8.5
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Object.defineProperties' in that specification.
Standard  
ECMAScript Latest Draft (ECMA-262)
The definition of 'Object.defineProperties' in that specification.
Living Standard  

브라우저 지원 현황

Feature Firefox (Gecko) Chrome Edge Internet Explorer Opera Safari
Basic support 4.0 (2) 5 (Yes) 9 11.60 5
Feature Firefox Mobile (Gecko) Android Edge IE Mobile Opera Mobile Safari Mobile
Basic support 4.0 (2) (Yes) (Yes) ? 11.5 (Yes)

비슷한 문서

문서 태그 및 공헌자

 이 페이지의 공헌자: KangHyeongMin, devJang, BANIP
 최종 변경: KangHyeongMin,