-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdefine-decorator.js
More file actions
35 lines (33 loc) · 1.28 KB
/
define-decorator.js
File metadata and controls
35 lines (33 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
*
* @param {Object|Function} proto - The constructor function of a class or the prototype of a class
* @param {string} key - The name of the property or method where the decorator will be included
* @param {Function} decorator - The decorator to be included
*/
function defineDecorator(proto, key, decorator) {
if ((typeof proto !== 'object') && (typeof proto !== 'function')) {
throw new TypeError('Invalid prototype. Expected object or function.');
}
if (typeof key !== 'string') {
throw new TypeError('Invalid property name. Expected string or function.');
}
if (typeof decorator !== 'function') {
throw new TypeError('Invalid decorator. Expected function.');
}
decorator(proto, key, Object.getOwnPropertyDescriptor(proto, key));
}
//extend object
if (typeof Object.defineDecorator === 'undefined') {
/**
* @function defineDecorator
* @param {Object|Function} proto - The constructor function of a class or the prototype of a class
* @param {string} key - The name of the property or method where the decorator will be included
* @param {Function} decorator - The decorator to be included
* @memberOf Object
* @static
*/
Object.defineDecorator = defineDecorator;
}
module.exports = {
defineDecorator
};