A descriptor is an object which describe how the property behaves.
Simplest way to define an object is by using object literal, by doing so, the created object are set with implicit descriptors to all "true". As illustrated below:
var objectLiteral = {
name: 'foo'
};
// to see its descriptor
Object.getOwnPropertyDescriptor(objectLiteral, 'name');
// will log;
{
value: "foo",
writable: true,
enumerable: true,
configurable: true
}
"writable" property in descriptor controls the whether the properties' value can be changed. This is desired for creating variables behave as constant.
var foo = {};
Object.defineProperty(foo, 'name', {
value: 'foo',
writable: false // being explicit here, can also omit
});
foo.name = 'new foo';
foo.name; // still log 'foo' instead of 'new foo'