Skip to content

Instantly share code, notes, and snippets.

@fundon
Created January 5, 2011 03:48
Show Gist options
  • Save fundon/765901 to your computer and use it in GitHub Desktop.
Save fundon/765901 to your computer and use it in GitHub Desktop.
简单工厂模式(Simple Factory)
// javascript
function Shape(){};
Shape.prototype.draw = function(){};
function Circle(){};
Circle.prototype = new Shape();
function Square(){};
Square.prototype = new Shape();
function ArtTracer(){};
ArtTracer.factory = function(type){
var type_str = ({}).toString.call(type);
if(type == '[object Function'] && window[type]){
return new window[type]();
}else{
throw new Error('The type ' + type + ' undefined.');
}
};
var circle_obj = ArtTracer.factory('Circle');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment