From e1f59482c44a06464ba2415c9b8c0184164bd2d9 Mon Sep 17 00:00:00 2001 From: Aurimas Vinckevicius Date: Tue, 12 Aug 2014 00:19:07 -0500 Subject: [PATCH] Add Zotero.Utilities.defineProperty convenience method Use this to create enumerable properties in object prototypes. --- .../content/zotero/xpcom/utilities_internal.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/chrome/content/zotero/xpcom/utilities_internal.js b/chrome/content/zotero/xpcom/utilities_internal.js index d7bbc85d9b..adb31fc593 100644 --- a/chrome/content/zotero/xpcom/utilities_internal.js +++ b/chrome/content/zotero/xpcom/utilities_internal.js @@ -366,6 +366,24 @@ Zotero.Utilities.Internal = { 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); } }