Welcome 微信登录

首页 / 脚本样式 / JavaScript / javascript创建对象、对象继承的实用方式详解

本文约定:不特殊声明的情况下,属性代指属性或方法。
创建对象、对象继承实际上是一回事:我们所需要的实例对象通过构造函数获得私有属性、通过原型链获得共享的属性。什么是好的方式?私有属性通过构造函数的方式获得(不考虑实例中自定义私有属性)且不需要重写,共享属性通过原型链找到且不需要重复创建。
普适的方式

组合使用构造函数模式和原型模式创建对象

function HNU_student(name) {this.name = name;this.sayName = function() {return this.name;};}HNU_student.prototype = {school: "HNU",saySchool: function() {return this.school;}};Object.defineProperty(HNU_student, "constructor", {value: HNU_student});var hiyohoo = new HNU_student("xujian");
通过字面量的方式会重写prototype,且原型的constructor指向了Object,必要的情况下需要重新定义constructor。
寄生组合式继承

function object(o) {function F() {};F.prototype = o;return new F();}function inheritPrototype(child, parent) {var prototype = object(parent.prototype);prototype.constructor = child;child.prototype = prototype;}function HNU_student(name) {this.name = name;this.sayName = function() {return this.name;};}HNU_student.prototype.school = "HNU";HNU_student.prototype.saySchool = function() {return this.school;};function Student_2011(name, number) {HNU_student.call(this, name);this.number = number;this.sayNumber = function() {return this.number;}}inheritPrototype(Student_2011, HNU_student);Student_2011.prototype.graduationTime = 2015;Student_2011.prototype.sayGraduationTime = function() {return this.graduationTime;};var hiyohoo = new Student_2011("xujian", 20110803203);
object()的作用:将作为参数传入的对象变成实例的原型,该对象的属性被所有实例共享。
共享属性:inheritPrototype(Student_2011, HNU_student);,子构造函数原型成为超构造函数原型的一个实例,超构造函数原型中的属性共享给子构造函数。
私有属性:HNU_student.call(this, name);,通过子构造函数创建实例时调用超构造函数创建私有属性。
创建对象的其他方式

动态原型模式

function HNU_student(name) {this.name = name;this.sayName = function() {return this.name;};if (!HNU_student.prototype.school) {HNU_student.prototype.school = "HNU";HNU_student.prototype.saySchool = function() {return this.school;};}}var hiyohoo = new HNU_student("xujian");
将定义在原型中的共享属性放入构造函数中,使用判断语句,在第一次调用构造函数创建实例时,初始化原型共享属性。
寄生构造函数模式

function SpecialArray() {var values = new Array();values.push.apply(values, arguments);values.toPipedString = function() {return this.join("|");};return values;}var colors = new SpecialArray("red", "black", "white");
用于为原生构造函数添加特殊的属性。
对象继承的其他方式

组合继承

function HNU_student(name) {this.name = name;this.sayName = function() {return this.name;};}HNU_student.prototype.school = "HNU";HNU_student.prototype.saySchool = function() {return this.school;};function Student_2011(name, number) {HNU_student.call(this, name);this.number = number;this.sayNumber = function() {return this.number;};}Student_2011.prototype = new HNU_student();Student_2011.prototype.constructor = Student_2011;Student_2011.prototype.graduationTime = 2015;Student_2011.prototype.sayGraduationTime = function() {return this.graduationTime;}var hiyohoo = new Student_2011("xujian", 20110803203);
共享属性:Student_2011.prototype = new HNU_student();,子构造函数的原型就指向了超构造函数的原型,实例通过原型链找到所有共享的属性。
私有属性:HNU_student.call(this, name);,通过子构造函数创建实例时调用超构造函数创建私有属性。
缺陷:超构造函数被调用了两遍。Student_2011.prototype = new HNU_student();的同时,在子构造函数原型中创建了超构造函数定义的私有属性,这些原型中的私有属性被实例中的同名属性覆盖屏蔽。
原型式继承、寄生式继承

function object(o) {function F() {}F.prototype = o;return new F();}var student1 = {school: "HNU",saySchool: function() {return this.school;}};var student2 = object(student1);
Object.creat()是ECMAScript5新增的方法,接受两个参数:一是作为原型的原对象,二是重写或新增属性的对象,作用与自定义的object()相同。
var student1 = {name: "xujian",school: "HNU"};var student2 = Object.create(student1, {name: {value: "huangjing"}});
寄生式继承在原型式继承的基础上添加了额外的属性用来增强对象。
function object(o) {function F() {}F.prototype = o;return new F();}function creatAnother(original) {var clone = object(original);clone.sayHi = function() {alert("Hi!");};return clone;}var student1 = {school: "HNU",saySchool: function() {return this.school;}};var student2 = creatAnother(student1);
原型式继承和寄生式继承用于创建与已有对象类似的实例对象。