Skip to content

Instantly share code, notes, and snippets.

@edoardocavazza
Last active April 12, 2024 14:24
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edoardocavazza/47246856759f2273e48b to your computer and use it in GitHub Desktop.
Save edoardocavazza/47246856759f2273e48b to your computer and use it in GitHub Desktop.
An "Object.setPrototypeOf" polyfill for IE9
// https://gist.github.com/edoardocavazza/47246856759f2273e48b
(function () {
if (typeof Object.setPrototypeOf === 'undefined' && typeof Object.getOwnPropertyNames === 'function') {
var _exclude = ['length', 'name', 'arguments', 'caller', 'prototype'];
function bindFunction(ctx, fn) {
return function() {
return fn.apply(this, arguments);
}
}
function bindProperty(ctx, prop, parentDescriptor) {
if (!parentDescriptor) {
var defaultValue = ctx.__proto__[prop];
parentDescriptor = {
get: function () {
return ctx['__' + prop] || defaultValue
},
set: function (val) {
ctx['__' + prop] = val;
}
}
}
Object.defineProperty(ctx, prop, {
get: parentDescriptor.get ? parentDescriptor.get.bind(ctx) : undefined,
set: parentDescriptor.set ? parentDescriptor.set.bind(ctx) : undefined,
configurable: true
});
}
function iterateProps(subClass, superClass) {
var props = Object.getOwnPropertyNames(superClass),
proto;
subClass.__proto__ = superClass;
for (var i = 0, len = props.length; i < len; i++) {
var prop = props[i];
if (prop === '__proto__') {
proto = superClass[prop];
} else if (_exclude.indexOf(i) === -1) {
var descriptor = Object.getOwnPropertyDescriptor(subClass, prop);
if (!descriptor) {
var superDescriptor = Object.getOwnPropertyDescriptor(superClass, prop);
if (typeof superDescriptor.get !== 'function' && typeof superClass[prop] === 'function') {
subClass[prop] = bindFunction(subClass, superClass[prop]);
} else if (typeof superDescriptor.get == 'function') {
bindProperty(subClass, prop, superDescriptor);
} else {
bindProperty(subClass, prop);
}
}
}
}
if (proto) {
iterateProps(subClass, proto);
}
}
Object.setPrototypeOf = iterateProps;
}
})();
@FuuuOverclocking
Copy link

I doubt that the line 41 should be changed to

} else if (_exclude.indexOf(prop) === -1) {

or in a form of bitwise

} else if (!~_exclude.indexOf(prop)) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment