Skip to content

Commit

Permalink
Ignore get/set/frozen property descriptors, Require Node.js 10
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyfarrell authored and isaacs committed Feb 26, 2021
1 parent 3929680 commit 45eedc2
Show file tree
Hide file tree
Showing 4 changed files with 7,434 additions and 1,892 deletions.
39 changes: 27 additions & 12 deletions bind-obj-methods.js
Expand Up @@ -10,16 +10,31 @@ module.exports = (obj, proto, bound) => {
bound.constructor = true
proto = proto || obj

Object.keys(proto)
.filter(k => (typeof obj[k] === 'function' && !bound[k]))
.forEach(k => (bound[k] = true, obj[k] = proto[k].bind(obj)))

Object.getOwnPropertyNames(proto)
.filter(k => (typeof obj[k] === 'function' && !bound[k]))
.forEach(k => (bound[k] = true, Object.defineProperty(obj, k, {
value: obj[k].bind(obj),
enumerable: false,
configurable: true,
writable: true
})))
for (const k of Object.getOwnPropertyNames(proto)) {
if (bound[k]) {
continue
}

const descriptor = {...Object.getOwnPropertyDescriptor(proto, k)};

if ('value' in descriptor) {
if (typeof descriptor.value !== 'function') {
continue
}

descriptor.value = descriptor.value.bind(obj);
if (!descriptor.configurable) {
if (!descriptor.writable) {
continue;
}

obj[k] = descriptor.value;
bound[k] = true;
continue;
}

bound[k] = true;
Object.defineProperty(obj, k, descriptor)
}
}
}

0 comments on commit 45eedc2

Please sign in to comment.