Skip to content

Instantly share code, notes, and snippets.

@jimmont jimmont/extend.js

Created Jan 14, 2015
What would you like to do?
Extend something, experimental alternative to SomeClass.prototype = AnotherClass() then extend further.
function extend( proto, template ){
var prop;
for(prop in template){
if( typeof(template[prop]) === 'function' ){
// relay arguments, using predefined functions where possible
proto[ prop ] = proto[ prop ] || (function(prop){
return function(){ return this._xhr[ prop ].apply(this._xhr, arguments); };
Object.defineProperty(proto, prop, (function(prop){
return {
get: function(){ return this._xhr[prop]; }
,set: function(val){ return this._xhr[prop] = val; }
return template;
function FakeXMLHttpRequest(){ this._xhr = new XMLHttpRequest; };
FakeXMLHttpRequest.prototype = {eg: function(){ return Math.random(); }};
extend( FakeXMLHttpRequest.prototype, new XMLHttpRequest).constructor.prototype.constructor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.