js继承
发布日期:2021-05-11 00:25:42 浏览次数:13 分类:精选文章

本文共 4052 字,大约阅读时间需要 13 分钟。

###������������������������ ES6 ���������

####���������������������

���������������������������������������������������prototype������������������������������������������������������������ toString ������������������������ prototype��������������������� Sergeant ������������������������������������������������������������������������������������������������������������������

#####������������������������������������ inheritPrototype

function inheritPrototype(subType, superType) {    // ���������������������������������    const prototype = Object.create(superType.prototype);    // ���������������������������������������������������������constructor������    prototype.constructor = subType;    // ���������������������    subType.prototype = prototype;}

������������������������������������ Father������������������������������������������������ sayName���

Father ������������������

function Father(name) {    this.name = name;    this.colors = ["red", "blue", "green"];}

Father ���������������

Father.prototype.sayName = function() {    alert(this.name);};

#####������������ Father���������������������������������������

��������������������������� Son������������������������������

  • ��������������� Father.call(this, name) ������ name ��� colors������
  • ������������ age ������������������������������ age ������
  • ������������������ sayAge()
  • Son ������������������

    function Son(name, age) {    Father.call(this, name);    this.age = age;}

    #####���������������������

    inheritPrototype(Son, Father);

    #####������������������

    Son.prototype.sayAge = function() {    alert(this.age);};

    #####������������

    const demo1 = new Son("TianTian", 21);const demo2 = new Son("TianTianUp", 20);// demo1.colors ������ ["red", "blue", "green"]// demo2.colors ������demo1.colors.push("2"); // ["red", "blue", "green", "2"]demo2.colors.push("3"); // ["red", "blue", "green", "3"]demo2.sayName(); // alert "TianTianUp"console.log(demo1 instanceof Son); // true

    ������������������������������������������������������������������������������������������������������������������ new ���������������������������������������������

    ####ES6 ���������

    ES6 ��������������������������������������������������������������������������������������������������������� Rectangle ���������������������

    #####Rectangle ���

    class Rectangle {    constructor(height, width) {        this.height = height;        this.width = width;    }    get area() {        return this.calcArea();    }    calcArea() {        return this.height * this.width;    }}

    #####������������

    const rectangle = new Rectangle(40, 20);// rectangle��� area ������������������������console.log(rectangle.area); // ������ 800

    #####������ Square ��������� Rectangle ���

    class Square extends Rectangle {    constructor(len) {        // ������������������ super ������������        super(len, len);        this.name = "Square";    }    get area() {        return this.height * this.width;    }}

    #####������������

    const square = new Square(20);console.log(square.area); //mina ��� 20 * 20 = 400

    ��������� Square ������������������ Rectangle ��� constructor ��������������������������� height ��� width��������������� len ������ length��������� Rectangle ��������������� area ������ Square ������������������������������������������������

    #####������������������������������������������

    ��������������������������������������������������������������������������������������������������������������������������������������������������� ES6 ������������������������������������������������������������������������������������������������

    ���������������������������������������������������������������ES6 ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    ��������������������������������������������� JavaScript ���������������������������������������������������������������������������������������������������������������������������������������������������������

    上一篇:彻底弄懂slice和splice的区别
    下一篇:js链式调用

    发表评论

    最新留言

    路过,博主的博客真漂亮。。
    [***.116.15.85]2025年04月17日 07时59分17秒