Skip to content

Instantly share code, notes, and snippets.

@jimmont jimmont/extend.js

Created Jan 14, 2015
Embed
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){
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
You can’t perform that action at this time.