commit 29e9946123fa46a8d7e9b3a74605e0d5431b8b1d
parent d4bd0ee81170c02c0adca39e28726330c50b47c5
Author: Aurimas Vinckevicius <aurimas.dev@gmail.com>
Date: Sun, 2 Nov 2014 13:47:52 -0600
Allow to define properties for late initialization via defineProperty
Diffstat:
1 file changed, 17 insertions(+), 1 deletion(-)
diff --git a/chrome/content/zotero/xpcom/zotero.js b/chrome/content/zotero/xpcom/zotero.js
@@ -1407,14 +1407,30 @@ Components.utils.import("resource://gre/modules/osfile.jsm");
* @param {Object} obj Target object
* @param {String} prop Property to be defined
* @param {Object} desc Propery descriptor. If not overriden, "enumerable" is true
+ * @param {Object} opts Options:
+ * lazy {Boolean} If true, the _getter_ is intended for late
+ * initialization of the property. The getter is replaced with a simple
+ * property once initialized.
*/
- this.defineProperty = function(obj, prop, desc) {
+ this.defineProperty = function(obj, prop, desc, opts) {
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];
}
+
+ if (opts) {
+ if (opts.lazy && d.get) {
+ let getter = d.get;
+ d.get = function() {
+ var val = getter.call(this);
+ this[prop] = val; // Replace getter with value
+ return val;
+ }
+ }
+ }
+
Object.defineProperty(obj, prop, d);
}