-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
33 lines (30 loc) · 854 Bytes
/
index.js
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
// @thanks to: https://github.com/andreypopp/autobind-decorator/blob/master/src/index.js
module.exports = function(inTarget, inKey, inDescriptor) {
var fn = inDescriptor.value;
console.log('typeof fn', typeof fn);
if (typeof fn !== 'function') {
throw new TypeError('@autobind decorator can only be applied to methods');
}
return {
configurable: true,
get: function() {
if (this.hasOwnProperty(inKey)) return fn;
var boundFn = fn.bind(this);
// redefine the key for the target:
Object.defineProperty(this, inKey, {
configurable: true,
get: function() {
return boundFn;
},
set: function(inValue) {
fn = inValue;
delete this[inKey];
}
});
return boundFn;
},
set: function(inValue) {
fn = inValue;
}
};
};