Object.defineProperty() Javascript

There will be some situations where we should not allow our objects to be override or delete etc. Then we have to use this defineProperty.

The Object.defineProperty() method defines a new property directly on an object, or modifies an existing property on an object, and returns the object.

Syntax

Object.defineProperty(obj, prop, descriptor)

Parameters

obj
The object on which to define the property.
prop
The name of the property to be defined or modified.
descriptor
The descriptor for the property being defined or modified.

When do you use?

Object.defineProperty is mainly used to set properties with specific property descriptors (e.g. read-only (constants), enumerability (to not show a property in a for (.. in ..) loop, getters, setters).

In Javascript, the standard properties (data member with getter and setter described above) are defined by accessor descriptor (in the link you have in your question). Exclusively, you can use data descriptor (so you can’t use i.e. value and set on the same property):

  • accessor descriptor = get + set (see the example above)
    • get must be a function; its return value is used in reading the property; if not specified, the default is undefined, which behaves like a function that returns undefined
    • set must be a function; its parameter is filled with RHS in assigning a value to property; if not specified, the default is undefined, which behaves like an empty function
  • data descriptor = value + writable (see the example below)
    • value default undefined; if writable, configurable and enumerable (see below) are true, the property behaves like an ordinary data field
    • writable – default false; if not true, the property is read only; attempt to write is ignored without error*!

Both descriptors can have these members:

  • configurable – default false; if not true, the property can’t be deleted; attempt to delete is ignored without error*!
  • enumerable – default false; if true, it will be iterated in for(var i in theObject); if false, it will not be iterated, but it is still accessible as public

* unless in strict mode – in that case JS stops execution with TypeError unless it is caught in try-catch block

Example:

var createPerson = function(firstName,lastName){
var person = {};
Object.defineProperty(person,”firstName”,{
value:firstName,
writable:false
});
Object.defineProperty(person,”lastName”,{
value:lastName,
writable:true
});
return person;
};

var person = createPerson(“sathya”,”vakacharla”);

Instead you can use object.defineProperties to set properties for firstName and lastName at a time.,

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s