diff --git a/.babelrc b/.babelrc index 55dee01..8d73751 100644 --- a/.babelrc +++ b/.babelrc @@ -1,6 +1,6 @@ { "modules": "umd", - "loose": "all", + "loose": false, "compact": true, "comments": false, "stage": 0 diff --git a/src/decko.js b/src/decko.js index 298e092..3013a19 100644 --- a/src/decko.js +++ b/src/decko.js @@ -33,11 +33,13 @@ let fns = { configurable: true, get() { let value = fn.bind(this); - Object.defineProperty(this, key, { - value, - configurable: true, - writable: true - }); + if (target.constructor === this.constructor) { + Object.defineProperty(this, key, { + value, + configurable: true, + writable: true + }); + } return value; } }; diff --git a/tests/bind.js b/tests/bind.js index c76a0f4..d522e22 100644 --- a/tests/bind.js +++ b/tests/bind.js @@ -34,4 +34,38 @@ describe('bind()', () => { next(); }); + + it('should bind when used as a decorator for a class method that invokes a decorated super method', next => { + let aValue = 'a', + bValue = 'b'; + + class A { + @bind + f() { + return {value: aValue, this: this}; + } + } + + class B extends A { + @bind + f() { + let superResult = super.f(); + return {value: bValue, superValue: superResult.value, this: this}; + } + } + + let b = new B(), + result; + + + // call twice to take into consideration effect of super method call + for (let i = 0; i < 2; i++) { + result = b.f(); + expect(result.this).to.equal(b); + expect(result.value).to.equal(bValue); + expect(result.superValue).to.equal(aValue); + } + + next(); + }); });