Skip to content

Commit 98968ee

Browse files
acrazingstevemao
authored andcommitted
fix(core): allow user set new value
closes #58 closes #42 closes #29
1 parent 421cfc5 commit 98968ee

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

src/__tests__/autobind-test.js

+54-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ describe('autobind method decorator', function() {
5757
}, /@autobind decorator can only be applied to methods/);
5858
});
5959

60-
61-
it('should not override binded instance method, while calling super method with the same name', function() { // eslint-disable-line max-len
60+
it('should not override bound instance method, while calling super method with the same name', function() { // eslint-disable-line max-len
6261
class B extends A {
6362

6463
@autobind
@@ -210,3 +209,56 @@ describe('autobind class decorator', function() {
210209
});
211210
});
212211
});
212+
213+
describe('set new value', function() {
214+
class A {
215+
constructor() {
216+
this.data = 'A';
217+
this.foo = 'foo';
218+
this.bar = 'bar';
219+
}
220+
221+
@autobind
222+
noop() {
223+
return this.data;
224+
}
225+
}
226+
227+
const a = new A();
228+
229+
it('should not throw when reassigning to an object', function () {
230+
a.noop = {
231+
foo: 'bar'
232+
};
233+
assert.deepEqual(a.noop, {
234+
foo: 'bar'
235+
});
236+
assert.equal(a.noop, a.noop);
237+
});
238+
239+
it('should not throw when reassigning to a function', function() {
240+
a.noop = function noop () {
241+
return this.foo;
242+
};
243+
assert.equal(a.noop(), 'foo');
244+
const noop = a.noop;
245+
assert.equal(noop(), 'foo');
246+
assert.equal(a.noop, a.noop);
247+
});
248+
249+
it('should not throw when reassigning to a function again', function() {
250+
a.noop = function noop2 () {
251+
return this.bar;
252+
};
253+
assert(a.noop(), 'bar');
254+
const noop2 = a.noop;
255+
assert.equal(noop2(), 'bar');
256+
assert.equal(a.noop, a.noop);
257+
});
258+
259+
it('should not throw when reassigning to an object after bound a function', function() {
260+
a.noop = {};
261+
assert.deepEqual(a.noop, {});
262+
assert.equal(a.noop, a.noop);
263+
});
264+
});

src/index.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,28 @@ function boundMethod(target, key, descriptor) {
7373
return {
7474
configurable: true,
7575
get() {
76-
if (definingProperty || this === target.prototype || this.hasOwnProperty(key)) {
76+
if (definingProperty || this === target.prototype || this.hasOwnProperty(key)
77+
|| typeof fn !== 'function') {
7778
return fn;
7879
}
7980

8081
let boundFn = fn.bind(this);
8182
definingProperty = true;
8283
Object.defineProperty(this, key, {
83-
value: boundFn,
8484
configurable: true,
85-
writable: true
85+
get() {
86+
return boundFn;
87+
},
88+
set(value) {
89+
fn = value;
90+
delete this[key];
91+
}
8692
});
8793
definingProperty = false;
8894
return boundFn;
95+
},
96+
set(value) {
97+
fn = value;
8998
}
9099
};
91100
}

0 commit comments

Comments
 (0)