Created
January 5, 2011 03:48
-
-
Save fundon/765901 to your computer and use it in GitHub Desktop.
简单工厂模式(Simple Factory)
This file contains 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
// 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