Object.decorate seems to fit the bill I think.
On another note, using an array and the as of yet non-standardized but
still useful function name property is a lot more palatable and succinct
and is much closer to the desired improved object literal syntax. I've been
partial to the following reimagining of Object.extend as Object.decorate
which supports arrays of named functions as well as objects and also
setters/getters which existing extend functions don't support usually.
Object.defineProperty(Object, 'decorate', {
configurable: true,
writable: true,
value: function decorate(o){
var a, b, c, d;
for (a in arguments) {
if (a) {
if (Array.isArray(b = arguments[a])) {
// array of named functions
for (c = 0; c < b.length; c++) {
if (typeof b[c] === 'function' && b[c].name) {
Object.defineProperty(o, b[c].name, { value: b[c],
configurable: true, writable: true });
}
}
} else {
// object
for (c in b) {
// use getDesc instead of hasOwn to support get/set
if (d = Object.getOwnPropertyDescriptor(b, c)) {
if (d.get || d.set) {
Object.defineProperty(o, c, d);
} else {
// purposefully trigger accessors if they exist
o[c] = d.value;
}
}
}
}
}
}
return o;
}
});
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss