1,工厂模式javascript
//工厂模式 function createObiect(name,age){ var obj=new Object(); //建立对象 obj.name=name; //添加属性 obj.age=age; obj.run=function(){ //添加对象方法 return this.name+this.age+'运行中....' }; return obj; //返回对象引用 } var box1=createObject('Lee',100); var box2=createObject('jact',200); alert(box1.run()); alert(box2.run()); //问题识别不出来,搞不清谁究竟是谁的对象
2, 构造函数模式java
fucntion Box(name,age){ this.name=name; this.age=age; this.run=function(){ return this.name+this.age+'运行中'; } } var box1=new Box('lee',100); var box2=new Box('jack',200); box1 instanceof Object //
使用构造函数和工厂模式的方法他们不一样之处以下:函数
《1,构造函数方法没有显示的建立对象(New Object())this
《2,直接将属性和方法赋值给this对象prototype
《3,构造函数没有return 对象,是后台自动返回的指针
3,构造函数和普通函数的区别code
构造函数调用必须使用new ,建立构造函数名首字母大写对象
4,对象冒充调用继承
fucntion Box(name,age){ this.name=name; this.age=age; this.run=function(){ return this.name+this.age+'运行中'; } } var o=new Object(); Box.call(o,'lee',100); //对象冒充,把Box的功能都拿给了o对象 alert(o.run())
//对象冒充 function Person(name,age){ this.name = name; this.age = age; this.show = function(){ console.log(this.name+", "+this.age); } } function Student(name,age){ this.student = Person; //将Person类的构造函数赋值给this.student this.student(name,age); //js中其实是经过对象冒充来实现继承的 delete this.student; //移除对Person的引用 } var s = new Student("小明",17); s.show(); var p = new Person("小花",18); p.show(); // 小明, 17 // 小花, 18
5,构造函数里的方法究竟是基本类型仍是引用类型ip
fucntion Box(name,age){ this.name=name; this.age=age; this.run=function(){ //return this.name+this.age+'运行中'; this.run=run; } } //注明全局name变量会存在问题,换个名字就能够,最好不要全局函数会 function run(){ //把构造函数内部的方法经过全局来实现引用地址一致 return this.name+this.age+'运行中'; } var box1=new Box('lee',100); //实例化后地址为1 var box2=new Box('lee',100); //实例化后地址为2 //alert(box1.name==box2.name); //true //alert(box1.age==box2.age); //true //alert(box1.run()==box2.run()); //true 构造函数体内的方法的值是至关的 alert(box1.run==box2.run); //false 由于他们比较的是引用地址
6,原型的缺点都是共享的,有些部分不但愿共享
//原型的缺点 //共享是优势也是缺点,若是box1和box2初始值不想同样的,因此这里是实现不了,都是共享的 function Box(){} Box.prototype={ constructor:Box, name:'lee', age:100, family:['哥哥','姐姐','妹妹'], run:function(){ return this.name+this.age+'运行中。。。' } } var box1=new Box(); alert(box1.family); //哥哥,姐姐,妹妹 box1.family.push('弟弟'); alert(box1.family); //哥哥,姐姐,妹妹,弟弟 var box2=new Box(); alert(box2.family); //哥哥,姐姐,妹妹,弟弟 //共享了box1添加后的引用的原型
7,组合构造函数+原型模式
//组合构造函数+原型模式 function Box(name,age){ //须要独立的用构造函数 this.name=name; this.age=age; this.family:['哥哥','姐姐','妹妹']; } Box.prototype={ //保持共享的用原型 constructor:Box, run:function(){ return this.name+this.age+'运行中。。。' } } var box1=new Box('lee',100); alert(box1.family); //哥哥,姐姐,妹妹 box.family.push('弟弟'); alert(box1.family); //哥哥,姐姐,妹妹,弟弟 var box2=new Box('jack',200); alert(box2.family) //哥哥,姐姐,妹妹
8,动态原型模式
//动态原型模式 //能够将原型封装到构造函数里 function Box(name,age){ this.name=name; this.age=age; this.family=['可可','姐姐','妹妹']; //这里的运行次数是几回呢? alert('原型初始化开始'); Box.prototype.run=function(){ return this.name+this.age+'运行中。。。' } alert('原型初始化结束'); } //原型的初始化,只要第一次初始化,就能够了,不必每次构造函数实例化的时候都初始化 var box1=new Box('lee',100); alert(box1.run()); var box2=new Box('jack',200); alert(box2.run());
如下解决初始化屡次的状况
function Box(name,age){ this.name=name; this.age=age; this.family=['可可','姐姐','妹妹']; if(typeof this.run !='function'){ //加上一个判断就能够解决屡次的初始化问题 Box.prototype.run=function(){ return this.name+this.age+'运行中。。。' } } } var box1=new Box('lee',100); alert(box1.run()); var box2=new Box('jack',200); alert(box2.run());
9,寄生构造函数=工厂模式+构造函数
//寄生构造函数=工厂模式+构造函数 function Box(name,age){ var obj=new Object(); obj.name=name; obj.run=function(){ return this.name+this.age+'运行中。。。' }; return obj; } var box1=new Box('Lee',100); alert(box1.run()); var box2=new Box('jack',200); alert(box2.run());
级寄生组合继承
//临时中转函数 function obj(o){ function F(){}; F.prototype=o; return new F(); } //寄生函数 function create(box,desk){ var f=obj(box.prototype); f.constructor=desk; //调整指针 desk.prototype=f; } function Box(name,age){ this.name=name; this.age=age; } Box.prototype.run=function(){ return this.name+this.age+'运行中。。。' } function Desk(name,age){ Box.call(this,name,age) } //经过寄生组合继承来实现继承 create(Box,Desk); //这句话用来替代Desk.prototype=new Box();