Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ import template from 'babel-template';
*/
const buildHelper = template(`
function HELPER(cls){
var setPrototypeOf = Object.setPrototypeOf || (function setPrototypeOf(a, b) {
a.__proto__ = b;
});
var getPrototypeOf = Object.getPrototypeOf || (function getPrototypeOf(a)) {
return a.__proto__;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't imagine a case where getPrototypeOf doesn't exist but __proto__ does. getPrototypeOf is from ES5, so it either exists, or there's no way to polyfill it AFAIK.

});
function ExtendableBuiltin(){
// Not passing "newTarget" because core-js would fall back to non-exotic
// object creation.
var instance = Reflect.construct(cls, Array.from(arguments));
Object.setPrototypeOf(instance, Object.getPrototypeOf(this));
var instance = new (Function.prototype.bind.apply(cls, arguments))();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The general concern with approaches like this is that .bind is known to be slow. Most things optimize this like statements like https://github.com/zloirock/core-js/blob/master/modules/es6.reflect.construct.js#L29, so larger code like that was what I was trying to avoid.

setPrototypeOf(instance, getPrototypeOf(this));
return instance;
}
ExtendableBuiltin.prototype = Object.create(cls.prototype, {
Expand All @@ -21,12 +27,8 @@ const buildHelper = template(`
configurable: true,
},
});
if (Object.setPrototypeOf){
Object.setPrototypeOf(ExtendableBuiltin, cls);
} else {
ExtendableBuiltin.__proto__ = cls;
}

setPrototypeOf(ExtendableBuiltin, cls);
return ExtendableBuiltin;
}
`);
Expand Down