Add Zotero.Utilities.defineProperty convenience method

Use this to create enumerable properties in object prototypes.
This commit is contained in:
Aurimas Vinckevicius 2014-08-12 00:19:07 -05:00
parent c5a532c789
commit e1f59482c4

View file

@ -366,6 +366,24 @@ Zotero.Utilities.Internal = {
break; break;
} }
} }
},
/**
* Defines property on the object's prototype.
* More compact way to do Object.defineProperty
*
* @param {Object} obj Target object
* @param {String} prop Property to be defined
* @param {Object} desc Propery descriptor. If not overriden, "enumerable" is true
*/
"defineProperty": function(obj, prop, desc) {
if (typeof prop != 'string') throw new Error("Property must be a string");
var d = { __proto__: null, enumerable: true }; // Enumerable by default
for (let p in desc) {
if (!desc.hasOwnProperty(p)) continue;
d[p] = desc[p];
}
Object.defineProperty(obj.prototype, prop, d);
} }
} }