工厂模式:用函数封装以特定接口创建对象;
/*函数封装*/function createPerson(name,age,job){ var o = new Object(); o.name = name; o.age = age; o.job = job; o.sayName = function(){ alert(this.name); }; return o; }var person1 = createPerson("wsc",25,"software Engineer");var person2 = createPerson("yl",26,"Doctor");
工厂模式解决的问题:相似对象的问题;没有解决的问题是对象识别的问题;