They are accessories for objects. They work efficiently when you get or set a property.
e.message; // getter e.message = "foobar"; // setter
Using property accessors, they do more than just get and set the value of the property. They can run code that has been set in the property descriptors of an object, so accessing properties can have side effects.
Example:
var o = Object.create(Object.prototype, { foobar: { get: function() { return "getter"; }, set: function(val) { alert("setter " + val); } } }); o.foobar;
To see the property descriptors set for this property, use Object.getOwnPropertyDescriptor
...
console.dir(Object.getOwnPropertyDescriptor(e, "message")); Object configurable: true enumerable: false get: function getter() { [native code] } set: function setter() { [native code] }
Note that these methods require support supported by ECMAScript 5.
user1106925
source share