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 = 2026;
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 = 2026;
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()为ECMAScript 5引入的新方法,接收两个参数:作为原型的源对象,以及用于重写或新增属性的对象。其作用等同于自定义的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);
原型式继承和寄生式继承旨在创建与现有对象结构相似的实例对象。