javascript创建对象、对象继承的实用方式详解
本文约定:不特殊声明的情况下,属性代指属性或方法。
创建对象、对象继承实际上是一回事:我们所需要的实例对象通过构造函数获得私有属性、通过原型链获得共享的属性。什么是好的方式?私有属性通过构造函数的方式获得(不考虑实例中自定义私有属性)且不需要重写,共享属性通过原型链找到且不需要重复创建。
普适的方式
组合使用构造函数模式和原型模式创建对象
functionHNU_student(name){ this.name=name; this.sayName=function(){ returnthis.name; }; } HNU_student.prototype={ school:'HNU', saySchool:function(){ returnthis.school; } }; Object.defineProperty(HNU_student,'constructor',{value:HNU_student}); varhiyohoo=newHNU_student('xujian');
通过字面量的方式会重写prototype,且原型的constructor指向了Object,必要的情况下需要重新定义constructor。
寄生组合式继承
functionobject(o){ functionF(){}; F.prototype=o; returnnewF(); } functioninheritPrototype(child,parent){ varprototype=object(parent.prototype); prototype.constructor=child; child.prototype=prototype; } functionHNU_student(name){ this.name=name; this.sayName=function(){ returnthis.name; }; } HNU_student.prototype.school='HNU'; HNU_student.prototype.saySchool=function(){ returnthis.school; }; functionStudent_2011(name,number){ HNU_student.call(this,name); this.number=number; this.sayNumber=function(){ returnthis.number; } } inheritPrototype(Student_2011,HNU_student); Student_2011.prototype.graduationTime=2015; Student_2011.prototype.sayGraduationTime=function(){ returnthis.graduationTime; }; varhiyohoo=newStudent_2011('xujian',20110803203);
object()的作用:将作为参数传入的对象变成实例的原型,该对象的属性被所有实例共享。
共享属性:inheritPrototype(Student_2011,HNU_student);,子构造函数原型成为超构造函数原型的一个实例,超构造函数原型中的属性共享给子构造函数。
私有属性:HNU_student.call(this,name);,通过子构造函数创建实例时调用超构造函数创建私有属性。
创建对象的其他方式
动态原型模式
functionHNU_student(name){ this.name=name; this.sayName=function(){ returnthis.name; }; if(!HNU_student.prototype.school){ HNU_student.prototype.school='HNU'; HNU_student.prototype.saySchool=function(){ returnthis.school; }; } } varhiyohoo=newHNU_student('xujian');
将定义在原型中的共享属性放入构造函数中,使用判断语句,在第一次调用构造函数创建实例时,初始化原型共享属性。
寄生构造函数模式
functionSpecialArray(){ varvalues=newArray(); values.push.apply(values,arguments); values.toPipedString=function(){ returnthis.join('|'); }; returnvalues; } varcolors=newSpecialArray('red','black','white');
用于为原生构造函数添加特殊的属性。
对象继承的其他方式
组合继承
functionHNU_student(name){ this.name=name; this.sayName=function(){ returnthis.name; }; } HNU_student.prototype.school='HNU'; HNU_student.prototype.saySchool=function(){ returnthis.school; }; functionStudent_2011(name,number){ HNU_student.call(this,name); this.number=number; this.sayNumber=function(){ returnthis.number; }; } Student_2011.prototype=newHNU_student(); Student_2011.prototype.constructor=Student_2011; Student_2011.prototype.graduationTime=2015; Student_2011.prototype.sayGraduationTime=function(){ returnthis.graduationTime; } varhiyohoo=newStudent_2011('xujian',20110803203);
共享属性:Student_2011.prototype=newHNU_student();,子构造函数的原型就指向了超构造函数的原型,实例通过原型链找到所有共享的属性。
私有属性:HNU_student.call(this,name);,通过子构造函数创建实例时调用超构造函数创建私有属性。
缺陷:超构造函数被调用了两遍。Student_2011.prototype=newHNU_student();的同时,在子构造函数原型中创建了超构造函数定义的私有属性,这些原型中的私有属性被实例中的同名属性覆盖屏蔽。
原型式继承、寄生式继承
functionobject(o){ functionF(){} F.prototype=o; returnnewF(); } varstudent1={ school:'HNU', saySchool:function(){ returnthis.school; } }; varstudent2=object(student1);
Object.creat()是ECMAScript5新增的方法,接受两个参数:一是作为原型的原对象,二是重写或新增属性的对象,作用与自定义的object()相同。
varstudent1={ name:'xujian', school:'HNU' }; varstudent2=Object.create(student1,{ name:{ value:'huangjing' } });
寄生式继承在原型式继承的基础上添加了额外的属性用来增强对象。
functionobject(o){ functionF(){} F.prototype=o; returnnewF(); } functioncreatAnother(original){ varclone=object(original); clone.sayHi=function(){ alert('Hi!'); }; returnclone; } varstudent1={ school:'HNU', saySchool:function(){ returnthis.school; } }; varstudent2=creatAnother(student1);
原型式继承和寄生式继承用于创建与已有对象类似的实例对象。