您的位置:首页 > Web前端 > JavaScript

2016.06.15廖雪峰JS__学习笔记(原型继承)__P9

2016-06-15 15:25 821 查看
原型继承

基于
Student
扩展出
PrimaryStudent


借助一个中间对象来实现正确的原型链,这个中间对象的原型要指向
Student.prototype


中间对象可以用一个空函数
F
来实现

// PrimaryStudent构造函数:
function PrimaryStudent(props) {
Student.call(this, props);
this.grade = props.grade || 1;
}

// 空函数F:
function F() {
}

// 把F的原型指向Student.prototype:
F.prototype = Student.prototype;

// 把PrimaryStudent的原型指向一个新的F对象,F对象的原型正好指向Student.prototype:
PrimaryStudent.prototype = new F();

// 把PrimaryStudent原型的构造函数修复为PrimaryStudent:
PrimaryStudent.prototype.constructor = PrimaryStudent;

// 继续在PrimaryStudent原型(就是new F()对象)上定义方法:
PrimaryStudent.prototype.getGrade = function () {
return this.grade;
};

// 创建xiaoming:
var xiaoming = new PrimaryStudent({
name: '小明',
grade: 2
});
xiaoming.name; // '小明'
xiaoming.grade; // 2

// 验证原型:
xiaoming.__proto__ === PrimaryStudent.prototype; // true
xiaoming.__proto__.__proto__ === Student.prototype; // true

// 验证继承关系:
xiaoming instanceof PrimaryStudent; // true
xiaoming instanceof Student; // true


用一张图来表示新的原型链:



注意,函数
F
仅用于桥接,我们仅创建了一个
new
F()
实例,而且,没有改变原有的
Student
定义的原型链。

如果把继承这个动作用一个
inherits()
函数封装起来,还可以隐藏
F
的定义,并简化代码:
function inherits(Child, Parent) {
var F = function () {};
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child;
}


这个
inherits()
函数可以复用:
function Student(props) {
this.name = props.name || 'Unnamed';
}

Student.prototype.hello = function () {
alert('Hello, ' + this.name + '!');
}

function PrimaryStudent(props) {
Student.call(this, props);
this.grade = props.grade || 1;
}

// 实现原型继承链:
inherits(PrimaryStudent, Student);

// 绑定其他方法到PrimaryStudent原型:
PrimaryStudent.prototype.getGrade = function () {
return this.grade;
};


小结

JavaScript的原型继承实现方式就是:

定义新的构造函数,并在内部用
call()
调用希望“继承”的构造函数,并绑定
this


借助中间函数
F
实现原型链继承,最好通过封装的
inherits
函数完成;

继续在新的构造函数的原型上定义新方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: