decorators/@autobind: Minor improvements.

This commit is contained in:
Julien Fontanet 2016-04-04 11:29:31 +02:00
parent 94703492fd
commit 3f6e5b7606

View File

@ -31,34 +31,30 @@ export const autobind = (target, key, {
enumerable, enumerable,
get () { get () {
const bounded = bind(fn, this) if (this === target) {
return fn
}
const bound = bind(fn, this)
defineProperty(this, key, { defineProperty(this, key, {
configurable: true, configurable: true,
enumerable: false, enumerable: false,
value: bounded, value: bound,
writable: true writable: true
}) })
return bounded return bound
}, },
set (newValue) { set (newValue) {
if (this === target) { // Cannot use assignment because it will call the setter on
// New value directly set on the prototype. // the prototype.
delete this[key] defineProperty(this, key, {
this[key] = newValue configurable: true,
} else { enumerable: true,
// New value set on a child object. 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
})
}
} }
}) })