您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

ECMAScript 6中类继承解析(附示例)

2025/9/29 11:22:30发布25次查看
本篇文章给大家带来的内容是关于ecmascript 6中类继承解析(附示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
类继承
看类继承前,先回顾构造函数怎么实现对象的继承的
        function f() {            this.a = 1;        }        function son() {            f.call(this);        }         function inherit(s, f) {            s.prototype = object.create(f.prototype);            s.prototype.constructor = s;        }        inherit(son, f);        let son = new son();
它实现了哪几个功能:
继承f的this属性也就是f实例对象的属性
son.prototype.__proto__ === f.prototype 实现了上下辈分的继承
son.constructor让son认祖归宗
同样类继承也是如此用来extends和super关键字,看一个简单的继承
        class a {            constructor() {                this.a = 1;            }        }        class b extends a {            constructor() {                super();                this.b = 2;            }            m() {            }        }        let b = new b();
同样实现了那三点基本功能
b {a: 1, b: 2}b.__proto__  == b.prototypeb.__proto__.__proto__ === a.prototypeb.constructor === b
我认为:关键字extends实现了原型的继承,以及constructor的修正;关键字super实现了父类this的继承,这里的super相当于a.prototype.constructor.call(this)
注意点写了constructor,就必须在里面写super,不然new子类实例对象会报错;要么都不写;其次子类的中constructor中的this属性必须写在super后面
1.es5 的继承,实质是先创造子类的实例对象this,然后再将父类的方法添加到this上面(parent.apply(this))。es6 的继承机制完全不同,实质是先将父类实例对象的属性和方法,加到this上面(所以必须先调用super方法),然后再用子类的构造函数修改this
2.因为子类自己的this对象,必须先通过父类的构造函数完成塑造,得到与父类同样的实例属性和方法,然后再对其进行加工,加上子类自己的实    例属性和方法。如果不调用super方法,子类就得不到this对象。
        class b extends a {            constructor() {    //要么都不写,new时默认会自动生成                super();                this.b = 2;    //写在super后面            }             m() {            }        }
super的各种指向问题super作为函数,只能放在子类的constructor中,指向a.prototype.constructor.call(this)
super作为对象,在子类普通方法中调用,super就是父类的原型也就是a.prototype;所以只能调用原型链上的方法,不能动用父类实例的方法和属性constructor{}中的不能调用
        class a {            constructor() {                this.a = 1;            }            n() {                return this;            }        }        class b extends a {            constructor() {                                super();                this.b = 2;                            }            m() {                return super.n();            }        }        let b = new b();        b === b.m();
并且规定
在子类普通方法中通过super调用父类的方法时,方法内部的this指向当前的子类实例。
所以上面return this 就是返回子类实例对象
super作为对象对属性赋值时
super相当于this,赋值属性也就成了子类实例的属性
class a {  constructor() {    this.x = 1;  }}class b extends a {  constructor() {    super();    this.x = 2;    super.x = 3;    console.log(super.x); // undefined    console.log(this.x); // 3    console.log(super.valueof() instanceof b);   //true  }}let b = new b();
super作为对象,在静态方法中指向的是父类能调用父类的静态方法,如果方法内部有this则指向当前的子类
只有类才能调用类的静态方法
        class a {            constructor() {                this.a = 1;            }            static n() {                return this;            }        }        class b extends a {            constructor() {                                super();                this.b = 2;                            }            static m() {                return super.n();            }        }        console.log(a.n() === a)   // true        console.log(b === b.m());  //true
由于对象总是继承其他对象的,所以可以在任意一个对象中,使用super关键字。var obj = {  tostring() {    return myobject:  + super.tostring();  }};object.getprototypeof(obj).tostring = function () {    return 这里super等于obj.__proto__;}console.log(obj.tostring());        //myobject: 这里super等于obj.__proto__
类的prototype与__proto__(1)子类的__proto__属性,表示构造函数的继承,总是指向父类。
(2)子类prototype属性的__proto__属性,表示方法的继承,总是指向父类的prototype属性。
类的继承模式
class a {}class b {}// b 的实例继承 a 的实例object.setprototypeof(b.prototype, a.prototype);// b 继承 a 的静态属性object.setprototypeof(b, a);const b = new b();
也是因为这种实现所以类能调用自己的静态方法
es6实现了原始构造函数的继承之前array.apply(this)this并不会塑造array里面的内部结构,所以我们当我们用类数组对象引用数组方法时用null代替了
而es6用类实现它的继承,
代码摘自es6入门
class myarray extends array {  constructor(...args) {    super(...args);  }}var arr = new myarray();arr[0] = 12;arr.length // 1arr.length = 0;arr[0] // undefined
需要注意的是
es6 改变了object构造函数的行为,一旦发现object方法不是通过new object()这种形式调用,es6 规定object构造函数会忽略参数。
class newobj extends object{  constructor(){    super(...arguments);  }}var o = new newobj({attr: true});o.attr === true  // false
传入参数会无效的
以上就是ecmascript 6中类继承解析(附示例)的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product