From 3f6e5b76066673234eecbef0027d671997c42ff1 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Mon, 4 Apr 2016 11:29:31 +0200 Subject: [PATCH] decorators/@autobind: Minor improvements. --- src/decorators.js | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/src/decorators.js b/src/decorators.js index b30c3d9b0..c2b190378 100644 --- a/src/decorators.js +++ b/src/decorators.js @@ -31,34 +31,30 @@ export const autobind = (target, key, { enumerable, get () { - const bounded = bind(fn, this) + if (this === target) { + return fn + } + + const bound = bind(fn, this) defineProperty(this, key, { configurable: true, enumerable: false, - value: bounded, + value: bound, writable: true }) - return bounded + return bound }, set (newValue) { - if (this === target) { - // New value directly set on the prototype. - delete this[key] - this[key] = newValue - } else { - // New value set on a child object. - - // Cannot use assignment because it will call the setter on - // the prototype. - defineProperty(this, key, { - configurable: true, - enumerable: true, - value: newValue, - writable: true - }) - } + // Cannot use assignment because it will call the setter on + // the prototype. + defineProperty(this, key, { + configurable: true, + enumerable: true, + value: newValue, + writable: true + }) } })