|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); |
| 4 | + |
| 5 | +var _dec, _desc, _value, _class; |
| 6 | + |
| 7 | +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } |
| 8 | + |
| 9 | +function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { |
| 10 | + var desc = {}; |
| 11 | + Object['ke' + 'ys'](descriptor).forEach(function (key) { |
| 12 | + desc[key] = descriptor[key]; |
| 13 | + }); |
| 14 | + desc.enumerable = !!desc.enumerable; |
| 15 | + desc.configurable = !!desc.configurable; |
| 16 | + |
| 17 | + if ('value' in desc || desc.initializer) { |
| 18 | + desc.writable = true; |
| 19 | + } |
| 20 | + |
| 21 | + desc = decorators.slice().reverse().reduce(function (desc, decorator) { |
| 22 | + return decorator(target, property, desc) || desc; |
| 23 | + }, desc); |
| 24 | + |
| 25 | + if (context && desc.initializer !== void 0) { |
| 26 | + desc.value = desc.initializer ? desc.initializer.call(context) : void 0; |
| 27 | + desc.initializer = undefined; |
| 28 | + } |
| 29 | + |
| 30 | + if (desc.initializer === void 0) { |
| 31 | + Object['define' + 'Property'](target, property, desc); |
| 32 | + desc = null; |
| 33 | + } |
| 34 | + |
| 35 | + return desc; |
| 36 | +} |
| 37 | + |
| 38 | +function fluent(target, name, descriptor) { |
| 39 | + var fn = descriptor.value; |
| 40 | + descriptor.value = function () { |
| 41 | + for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { |
| 42 | + args[_key] = arguments[_key]; |
| 43 | + } |
| 44 | + |
| 45 | + fn.apply(target, args); |
| 46 | + return target; |
| 47 | + }; |
| 48 | +} |
| 49 | + |
| 50 | +function decorateWith(decorator) { |
| 51 | + return function (target, name, descriptor) { |
| 52 | + descriptor.value = decorator.call(target, descriptor.value); |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +function describe(description) { |
| 57 | + return function (target, name) { |
| 58 | + target.description = target.description || {}; |
| 59 | + target.description[name] = description; |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +var Person = (_dec = describe('Set the name of person'), (_class = function () { |
| 64 | + function Person() { |
| 65 | + _classCallCheck(this, Person); |
| 66 | + } |
| 67 | + |
| 68 | + _createClass(Person, [{ |
| 69 | + key: 'setName', |
| 70 | + value: function setName(first, last) { |
| 71 | + this.first = first; |
| 72 | + this.last = last; |
| 73 | + } |
| 74 | + }, { |
| 75 | + key: 'sayName', |
| 76 | + value: function sayName() { |
| 77 | + console.log(this.first, this.last); |
| 78 | + } |
| 79 | + }]); |
| 80 | + |
| 81 | + return Person; |
| 82 | +}(), (_applyDecoratedDescriptor(_class.prototype, 'setName', [fluent, _dec], Object.getOwnPropertyDescriptor(_class.prototype, 'setName'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'sayName', [fluent], Object.getOwnPropertyDescriptor(_class.prototype, 'sayName'), _class.prototype)), _class)); |
| 83 | + |
| 84 | + |
| 85 | +var p = new Person(); |
| 86 | +p.setName('Jane', 'Doe').sayName().setName('John', 'Doe').sayName(); |
| 87 | + |
| 88 | +var desc = Person.prototype.description.setName; |
| 89 | +console.log(desc); |
0 commit comments