Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Configurable and Writable attributes of an Object

I saw the following regarding javascript, object data property attributes

— 
Configurable: Specifies whether the property can be deleted or changed.

— Enumerable: Specifies whether the property can be returned in a for/in loop.

— Writable: Specifies whether the property can be changed.

Here "Configurable" and "Writable" are representing the same (whether the property can be changed), then why do we need two separate attributes?

like image 369
Bravo Avatar asked May 11 '14 08:05

Bravo


People also ask

What is configurable object in JavaScript?

configurable: if false, the property cannot be removed nor any attribute can be changed, except its value. enumerable: true if the property is enumerable. get: a getter function for the property, called when the property is read. set: a setter function for the property, called when the property is set to a value.

What is object writable?

A polymorphic Writable that writes an instance with it's class name. Handles arrays, strings and primitive types without a Writable wrapper.

What is the difference between GET and defineProperty?

Using getter syntax you create a property which, prior to ES2015, you had to know the name of at the time that you were writing the code. Object. defineProperty allows you to perform the same as the above but, even before ES2015, does not require you to know the name of the property in advanced.

What is enumerable property in object?

Enumerable properties are those properties whose internal enumerable flag is set to true, which is the default for properties created via simple assignment or via a property initializer. Properties defined via Object.


2 Answers

From: http://ejohn.org/blog/ecmascript-5-objects-and-properties/

Writable: If false, the value of the property can not be changed.

Configurable: If false, any attempts to delete the property or change its attributes (Writable, Configurable, or Enumerable) will fail.

Enumerable: If true, the property will be iterated over when a user does for (var prop in obj){} (or similar).

like image 147
ColdCold Avatar answered Oct 14 '22 13:10

ColdCold


configurable and writable are NOT representing the same thing.

configurable means property descriptor and existence.

writable means property value only.

A property's descriptor contains value, enumerable, configurable and writable.

scenario 1: create property by assignment

'use strict';  // non-strict mode behaves slightly different  var foo = {}; foo.bar = 1;  // operated by CreateDataProperty*  // the above is the same as Object.defineProperty(foo, 'bar', {   value: 1,   configurable: true,   writable: true,   // ... }); 
  • CreateDataProperty is an operation defined together with ECMAScript spec.

scenario 2: create property by descriptor

'use strict';  // non-strict mode behaves slightly different  var foo = {}; Object.defineProperty(foo, 'bar', {   value: 1,   // configurable => false   // writable => false });  foo.bar = 2;    // throw TypeError: Cannot assign to read only property  Object.defineProperty(foo, 'bar', {   value: 2   // ... }); // throw TypeError: Cannot redefine property  delete foo.bar; // throw TypeError: Cannot delete property 
like image 41
themefield Avatar answered Oct 14 '22 12:10

themefield