<script type="text/javascript">
//简单工厂模式
//定义:由一个方法来决定到底要创建哪个类的实例,而这些实例经常拥有相同的接口。其实例在编译的时候不确定,而是在执行的时候确定。
var request1=Request("cgi.xx.com/xxx","get");
request1.start();
request1.done(fn);
var request2=Request("cgi.xx.com//xxx","jsonp");
request2.start();
request2.done(fn);
//new 构造器 实际上就是简单工厂模式
function A (name) {
this.name=name;
}
function ObjectFactory(){
var obj={},
Constructor=Array.prototype.shift.call(arguments);
obj.__proto__=typeof Constructor.prototype==="number"?Object.prototype:Constructor.prototype;
var ret=Constructor.apply(obj,arguments);
return typeof ret==="object"?ret:obj;
}
var a=ObjectFactory(A,"svenzeng");
alert(a.name);
</script>