javascript创建对象、对象继承的实用方式详解
作者:bea
本文约定:不特殊声明的情况下,属性代指属性或方法。 创建对象、对象继承实际上是一回事:我们所需要的实例对象通过构造函数获得私有属性、通过原型链获得共享的属性。什么是好的方式?私有属性通过构造函数的方式获得(不考虑实例中自定义私有属性)且不需要重写,共享属性通过原型链找到且不需要重复创建。 普适的方式 组合使用构造函数模式和原型模式创建对象 function HNU_student(name) { this.name = name; this.sayName = f
本文约定:不特殊声明的情况下,属性代指属性或方法。
创建对象、对象继承实际上是一回事:我们所需要的实例对象通过构造函数获得私有属性、通过原型链获得共享的属性。什么是好的方式?私有属性通过构造函数的方式获得(不考虑实例中自定义私有属性)且不需要重写,共享属性通过原型链找到且不需要重复创建。
普适的方式
组合使用构造函数模式和原型模式创建对象
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);
原型式继承和寄生式继承用于创建与已有对象类似的实例对象。
有用 | 无用
创建对象、对象继承实际上是一回事:我们所需要的实例对象通过构造函数获得私有属性、通过原型链获得共享的属性。什么是好的方式?私有属性通过构造函数的方式获得(不考虑实例中自定义私有属性)且不需要重写,共享属性通过原型链找到且不需要重复创建。
普适的方式
组合使用构造函数模式和原型模式创建对象
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);
原型式继承和寄生式继承用于创建与已有对象类似的实例对象。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- javascript事件绑定学习要点
- js实现分割上传大文件
- js实现ctrl+v粘贴上传图片(兼容chrome、firefox、ie11)
- 基于jQuery的网页影音播放器jPlayer的基本使用教程
- 利用jQuery设计一个简单的web音乐播放器的实例分享
- Bootstrap~多级导航(级联导航)的实现效果【附代码】
- js实现数组冒泡排序、快速排序原理
- Bootstrap多级导航栏(级联导航)的实现代码
- javascript html实现网页版日历代码
- 一道关于JavaScript变量作用域的面试题
- 理解javascript函数式编程中的闭包(closure)
- jQuery实现带水平滑杆的焦点图动画插件
- javascript对象的创建和访问
- js获取当前日期时间及其它日期操作汇总
- 使用JQuery实现智能表单验证功能
- js表单处理中单选、多选、选择框值的获取及表单的序列化
- JS实现设置ff与ie元素绝对位置的方法
- jQuery控制frames及frame页面JS的方法
- 三种Node.js写文件的方式