Built-In Class Object
Extensions to built in Object class.
| Method Attributes | Method Name and Description |
|---|---|
| <static> |
Object.defineProperty(obj, name, description)
Define a property on an object with flags. |
Define a property on an object with flags. This allows you to create read-only or non-enumerable properties on your javascript objects.
- Parameters:
- obj
Object on which to define the property
- name
property name
- description
Property descriptor.
A property descriptor is an object with some of the following keys
All values default to false or undefined. If either writable or value exist then neither getter nor setter are allowed.
A getter function is one which is called and should return the current value of the property. A setter is called with the argument to set the property too, but you can set it do a derivative of that value, or ignore it completly. Both getter and setter are called with "this" set to the object in question.
function MyObj() { var prop = 0; Object.defineProperty( myobj, "prop", { getter: function() { return prop }, setter: function(x) { prop = x * 2; return prop; }, enumerable: false, configurable: true } ); }If configurable is false, then any further attempts to call #defineProperty on that property will result in an exception.