[js] 设计模式 OO 工厂模式
# 工厂模式
缺点: 不能识别对象类型
1 <!DOCTYPE html> 2 <html> 3 4 <head> 5 <meta charset="UTF-8"> 6 <title></title> 7 </head> 8 9 <body> 10 <script> 11 function createPerson(name, age, job) { 12 var o = new Object(); 13 o.name = name; 14 o.age = age; 15 o.job = job; 16 o.sayName = function() { 17 console.log(this.name); 18 }; 19 return o; 20 } 21 var person1 = createPerson("Nicholas", 29, "Software Engineer");//没用new进行实例化 22 var person2 = createPerson("Greg", 27, "Doctor"); 23 person1.sayName(); //"Nicholas" 24 person2.sayName(); //"Greg" 25 </script> 26 </body> 27 28 </html>