Add Zotero.Utilities.defineProperty convenience method
Use this to create enumerable properties in object prototypes.
This commit is contained in:
parent
c5a532c789
commit
e1f59482c4
1 changed files with 18 additions and 0 deletions
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue