Created
April 10, 2011 20:15
-
-
Save juandopazo/912689 to your computer and use it in GitHub Desktop.
Something similar to a Strategy pattern?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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