Skip to content

Instantly share code, notes, and snippets.

@jimmont
Created January 14, 2015 23:03
Show Gist options
  • Save jimmont/7f975fc8238155b5c0d8 to your computer and use it in GitHub Desktop.
Save jimmont/7f975fc8238155b5c0d8 to your computer and use it in GitHub Desktop.
Extend something, experimental alternative to SomeClass.prototype = AnotherClass() then extend further.
function extend( proto, template ){
var prop;
for(prop in template){
try{
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); };
})(prop);
continue;
};
Object.defineProperty(proto, prop, (function(prop){
return {
get: function(){ return this._xhr[prop]; }
,set: function(val){ return this._xhr[prop] = val; }
};
})(prop));
}catch(err){};
};
return template;
};
function FakeXMLHttpRequest(){ this._xhr = new XMLHttpRequest; };
FakeXMLHttpRequest.prototype = {eg: function(){ return Math.random(); }};
extend(FakeXMLHttpRequest.prototype,
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