Skip to content

Instantly share code, notes, and snippets.

@juandopazo
Created April 10, 2011 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save juandopazo/912689 to your computer and use it in GitHub Desktop.
Save juandopazo/912689 to your computer and use it in GitHub Desktop.
Something similar to a Strategy pattern?
/*
Let's say I want to create an object using different constructors according
to some environmental condition (browser, available API, etc).
So I have two constructors, for instance for an Ajax object:
*/
function AjaxXMLHTTPRequest() {}
AjaxXMLHTTPRequest.prototype = {
// some methods
};
function AjaxActiveX() {}
AjaxActiveX.prototype = {
// same methods with different implementations
};
/*
How would you better use this constructors to abstract their differences?
I'm currently doing this:
*/
function Ajax() {
(/* some condition*/ ? AjaxXMLHTTPRequest : AjaxActiveX).apply(this, arguments);
}
extend(Ajax, /* some condition*/ ? AjaxXMLHTTPRequest : AjaxActiveX);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment