javascript设计模式

1工厂模式

工厂模式是软件领域一种广为人知的设计模式废话少说直接贴代码

 1 function createPerson(name,age,job){
2 var 0 = {};
3 o.name = name;
4 o.age = age;
5 o.job = job;
6 o.sayName = function(){
7 alert(this.name)
8 };
9 return o;
10 }
11 var person1 = createPerson("sunhaixun",29,"web");
12 var person2 = createPerson("kk",15,"doctor");
13 person1.sayName();
14 person2.sayName();

2.构造函数模式

 1 function Person(name,age,job){
2 this.name = name;
3 this.age = age;
4 this.job = job;
5 this.sayName = function(){
6 alert(this.name);
7 };
8 }
9 var person1 = newPerson("sunhaixun",29,"web");
10 var person2 = newPerson("zhanghaiwei",20,"doctor");
11 person1.sayName();
12 person2.sayName();

 

能力有限 其他的理解了在写

posted @ 2011-09-07 20:13  孙海勋  阅读(187)  评论(0编辑  收藏  举报