The pattern you're using looks good. It'll be supported by all ES5 browsers.
The get
and set
syntax inside the object literal is often referred to as ES5 object literal extensions for defining accessor properties. An accessor property is a property that is made up of a getter and/or setter. The term for the traditional kind of property which isn't a getter/setter is a data property.
And, yes, that is what kangax's compatibility table is referring to by "... in property initializer" (if you mouse-over the grey "c" with a circle around it on that page, you can see the actual test that's being run).
The meta-property features provided by Object.defineProperty
are referred to as property descriptors. There are two kinds of property descriptors in ES5: data descriptors and accessor descriptors, which are made up of the following descriptor properties:
Data Descriptor: value
, writable
, enumerable
, configurable
Example:
Object.defineProperty(obj, 'prop', {
value: 'some value',
writable: true,
enumerable: false,
configurable: true
});
Accessor Descriptor: get
, set
, enumerable
, configurable
Example:
Object.defineProperty(obj, 'prop', {
get: function() { return 'foo'; },
set: function() { /* do something... */ },
enumerable: false,
configurable: true
});