From 9700cdde387b28678e3e9cf085b5fa44ce2da4ac Mon Sep 17 00:00:00 2001 From: Aurimas Vinckevicius Date: Sun, 2 Nov 2014 13:48:45 -0600 Subject: [PATCH] Add Zotero.extendClass --- chrome/content/zotero/xpcom/zotero.js | 40 +++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/chrome/content/zotero/xpcom/zotero.js b/chrome/content/zotero/xpcom/zotero.js index 535eb2523..749bf3dd0 100644 --- a/chrome/content/zotero/xpcom/zotero.js +++ b/chrome/content/zotero/xpcom/zotero.js @@ -1613,6 +1613,46 @@ Components.utils.import("resource://gre/modules/osfile.jsm"); } + /** + * Defines property on the object + * 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 + * @param {Object} opts Options: + * lateInit {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, opts) { + if (typeof prop != 'string') throw new Error("Property must be a string"); + var d = { __proto__: null, enumerable: true, configurable: true }; // Enumerable by default + for (let p in desc) { + if (!desc.hasOwnProperty(p)) continue; + d[p] = desc[p]; + } + + if (opts) { + if (opts.lateInit && 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); + } + + this.extendClass = function(superClass, newClass) { + newClass._super = superClass; + newClass.prototype = Object.create(superClass.prototype); + newClass.prototype.constructor = newClass; + } + /** * Allow other events (e.g., UI updates) on main thread to be processed if necessary *