Skip to content
Advertisements

Object.defineProperties()

Why Object.defineProperties?

Object.defineProperties () creates or configures properties that are named and described by identifiers in the object. The names of the identifiers are property-generating objects that specify the properties of the properties to be created or the properties of the properties to be configured.

Syntax:

Object.defineProperties(obj, props)

obj: the object to which properties will be defined.
props: Creates an object with enumeration properties. Or create the identifiers of the properties to be modified.

Identifiers have the following characteristics:

  • configurable: If we don’t enter any property, it has the value “false”. true, and only if the type of this property identifier can be changed and the property can be deleted from the related object.
  • enumerable: If you don’t enter any property, it has the value “false”. We can change the properties by entering the True value.
  • value: The value of the property. number or a text. By default, undefined.
  • writable: if we don’t enter any property, it has the value “false”. true and only if the property-related value can be changed with an assignment operator.
  • get: If we don’t enter any property, it has the value “false”. A function that functions as a recipient for the property or is undefined if a recipient is not present. The return value of the function will be used as the value of the property.
  • set: If you do not enter any property, it has the value “false”. A function that functions as a setter for the property or is undefined if there is no setting. The function takes the new value assigned to the property as its single argument.

JavaScript Object.defineProperties Examples

Example 1


        let myObject = Object.defineProperties({}, {

            obj1: {
                value: 0,
                writable: false,
                enumerable: true,
                configurable: true
            },

            obj2: {
                value: 1,
                writable: false,
                enumerable: true,
                configurable: true
            }

        })

        console.log(myObject)

output:

{obj1: 0, obj2: 1}


Example 2

        
        let myObject = {};

        Object.defineProperties(myObject, {
            obj1: {
                value: "this is Object.defineProperties",
                writable: true
            },
            obj2: {}
        });

        console.log(myObject.obj1)

output:

this is Object.defineProperties


Example 3

        let obj = {
            name: "alex"
        };

        let myObj = Object.create(obj);

        Object.defineProperties(myObj, {
            surname: {
                value: "allen"
            }
        });

        console.log("name: " + myObj.name);
        console.log("surname: " + myObj.surname);

output:

name: alex
surname: allen


Polyfill


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;
}

Browser Support

Chrome5
Edgeyes
Firefox4
Internet Explorer9
Opera11.6
Safari5
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android4
Opera Android11.5
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: