Skip to content

Instantly share code, notes, and snippets.

@dead-claudia
Created January 27, 2015 03:57
Show Gist options
  • Save dead-claudia/da1d120e61cb88072716 to your computer and use it in GitHub Desktop.
Save dead-claudia/da1d120e61cb88072716 to your computer and use it in GitHub Desktop.
Object.prototype.__proto__ polyfill for ES6 (if the runtime *doesn't* implement it -- it's technically optional for non-web browsers)
/**
* By Isiah Meadows.
* This code is licensed under CC0 1.0 Universal. You can get a copy of the license at https://creativecommons.org/publicdomain/zero/1.0/legalcode.txt
*/
if (!function (F) {
return {}.__proto__ === Object.prototype; // Also if it's broken
}(function () {}) && Object.getPrototypeOf && Object.setPrototypeOf) {
Object.defineProperty(Object.prototype, '__proto__', {
'get': function () {
return Object.getPrototypeOf(this);
},
'set': function (proto) {
return Object.setPrototypeOf(this, proto);
}
});
}
/*By Isiah Meadows. This code is licensed under CC0 1.0 Universal. You can get a copy of the license at https://creativecommons.org/publicdomain/zero/1.0/legalcode.txt*/Object.prototype!=={}.__proto__&&Object.getPrototypeOf&&Object.setPrototypeOf&&Object.defineProperty(Object.prototype,'__proto__',{'set':function(p){return Object.setPrototypeOf(this,p)},'get':function(){return Object.getPrototypeOf(this)}});
@anonyco
Copy link

anonyco commented Jan 12, 2019

More performant version:

/**
 * By Isiah Meadows, then edited by Jack and 
 * This code is licensed under CC0 1.0 Universal. You can get a copy of the license at https://creativecommons.org/publicdomain/zero/1.0/legalcode.txt
 */

if (({}).__proto__ !== Object.prototype) (function(getPrototypeOf, setPrototypeOf){
    Object.defineProperty(Object.prototype, '__proto__', {
        'get': function () {
            return getPrototypeOf(this);
        },
        'set': function (proto) {
            return setPrototypeOf(this, proto);
        }
    });
})(Object.getPrototypeOf, Object.setPrototypeOf);

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