读书笔记-es6Class的继承
栏目: JavaScript · 发布时间: 8年前
内容简介:读书笔记-es6Class的继承
Class之间可以通过extends关键字实现继承,这比ES5的通过修改原型链实现继承,要清晰和方便很多。
classColorPointextendsPoint{
constructor(x, y, color) {
super(x, y); // 调用父类的constructor(x, y)
this.color = color;
}
toString() {
return this.color + ' ' + super.toString(); // 调用父类的toString()
}
}
子类必须在constructor方法中调用super方法,否则新建实例时会报错。这是因为子类没有自己的this对象,而是继承父类的this对象,然后对其进行加工。如果不调用super方法,子类就得不到this对象。
ES5的继承,实质是先创造子类的实例对象this,然后再将父类的方法添加到this上面(Parent.apply(this))。ES6的继承机制完全不同,实质是先创造父类的实例对象this(所以必须先调用super方法),然后再用子类的构造函数修改this。
在子类的构造函数中,只有调用super之后,才可以使用this关键字,否则会报错。这是因为子类实例的构建,是基于对父类实例加工,只有super方法才能返回父类实例。
classPoint{
constructor(x, y) {
this.x = x;
this.y = y;
}
}
classColorPointextendsPoint{
constructor(x, y, color) {
this.color = color; // ReferenceError
super(x, y);
this.color = color; // 正确
}
}
下面是生成子类实例的代码,这与ES5的行为完全一致。
let cp = new ColorPoint(25, 8, 'green'); cp instanceof ColorPoint // true cp instanceof Point // true
二、类的prototype属性和proto属性
大多数浏览器的ES5实现之中,每一个对象都有 proto 属性,指向对应的构造函数的prototype属性。Class作为构造函数的语法糖,同时有prototype属性和 proto 属性,因此同时存在两条继承链。
1)子类的 proto 属性,表示构造函数的继承,总是指向父类。
2)子类prototype属性的 proto 属性,表示方法的继承,总是指向父类的prototype属性。
classA{
}
classBextendsA{
}
B.__proto__ === A // true
B.prototype.__proto__ === A.prototype // true
三、Object.getPrototypeOf()
Object.getPrototypeOf方法可以用来从子类上获取父类。可以使用这个方法判断,一个类是否继承了另一个类。
Object.getPrototypeOf(ColorPoint) === Point // true
四、super 关键字
super这个关键字,既可以当作函数使用,也可以当作对象使用。在这两种情况下,它的用法完全不同。
1) super作为函数调用时,代表父类的构造函数。ES6 要求,子类的构造函数必须执行一次super函数。
classA{}
classBextendsA{
constructor() {
super();
}
}
注意,super虽然代表了父类A的构造函数,但是返回的是子类B的实例,即super内部的this指的是B,因此super()在这里相当于A.prototype.constructor.call(this)。
2) super作为对象时,在普通方法中,指向父类的原型对象;在静态方法中,指向父类。
classA{
p() {
return 2;
}
}
classBextendsA{
constructor() {
super();
console.log(super.p()); // 2
//super.p() 相当于 A.prototype.p()
}
}
let b = new B();
由于super指向父类的原型对象,所以定义在父类实例上的方法或属性,是无法通过super调用的。
classA{
constructor() {
this.p = 2;
}
}
classBextendsA{
get m() {
return super.p;
}
}
let b = new B();
b.m // undefined
如果属性定义在父类的原型对象上,super就可以取到。
classA{}
A.prototype.x = 2;
classBextendsA{
constructor() {
super();
console.log(super.x) // 2
}
}
let b = new B();
ES6 规定,通过super调用父类的方法时,super会绑定子类的this。
classA{
constructor() {
this.x = 1;
}
print() {
console.log(this.x);
}
}
classBextendsA{
constructor() {
super();
this.x = 2;
}
m() {
super.print();
}
}
let b = new B();
b.m() // 2
上面代码中,super.print()虽然调用的是A.prototype.print(),但是A.prototype.print()会绑定子类B的this,导致输出的是2,而不是1。也就是说,实际上执行的是super.print.call(this)。
五、实例的proto属性
子类实例的 proto 属性的 proto 属性,指向父类实例的 proto 属性。也就是说,子类的原型的原型,是父类的原型
var p1 = new Point(2, 3); var p2 = new ColorPoint(2, 3, 'red'); p2.__proto__ === p1.__proto__ // false p2.__proto__.__proto__ === p1.__proto__ // true
因此,通过子类实例的 proto . proto 属性,可以修改父类实例的行为。
p2.__proto__.__proto__.printName = function(){
console.log('Ha');
};
p1.printName() // "Ha"
上面代码在ColorPoint的实例p2上向Point类添加方法,结果影响到了Point的实例p1。
六、原生构造函数的继承
- Boolean()
- Number()
- String()
- Array()
- Date()
- Function()
- RegExp()
- Error()
- Object()
以前,这些原生构造函数是无法继承的,比如,不能自己定义一个Array的子类。ES6可以自定义原生数据结构(比如Array、String等)的子类,这是ES5无法做到的。
classVersionedArrayextendsArray{
constructor() {
super();
this.history = [[]];
}
commit() {
this.history.push(this.slice());
}
revert() {
this.splice(0, this.length, ...this.history[this.history.length - 1]);
}
}
var x = new VersionedArray();
x.push(1);
x.push(2);
x // [1, 2]
x.history // [[]]
x.commit();
x.history // [[], [1, 2]]
x.push(3);
x // [1, 2, 3]
x.revert();
x // [1, 2]
谢谢你请我吃糖果
支付宝
微信
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- ECMAScript学习笔记(七)——继承
- JS笔记(12): 对象的继承
- 【读书笔记】Effective C++(06)继承与面向对象
- 【 python 学习笔记 -- OOP】在实例中学习继承(inheritance)和组合(composition)
- 028.Python面向对象继承(单继承,多继承,super,菱形继承)
- PHP类继承、接口继承关系概述
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Operating System Algorithms
Nathan Adams、Elisha Chirchir / CreateSpace Independent Publishing Platform / 2017-4-21 / USD 39.15
Operating System Algorithms will walk you through in depth examples of algorithms that you would find in an operating system. Selected algorithms include process and disk scheduling.一起来看看 《Operating System Algorithms》 这本书的介绍吧!