• js实现继承的几种方式


    1 构造函数 

    function SuperType() {
    this.colors = ["red","blue","green"];
    }
    function SubType() {
    SuperType.call(this);//继承了SuperType
    }

    2 原型链

     

    function SuperType() {
    this.property = true;
    }
    SuperType.prototype.getSuperValue = function() {
    return this.property;
    }
    function subType() {
    this.property = false;
    }
    //继承了SuperType
    SubType.prototype = new SuperType();
    SubType.prototype.getSubValue = function (){
    return this.property;
    }
    var instance = new SubType();
    console.log(instance.getSuperValue());//true
    

    3 组合继承

    function SuperType(name) {
    this.name = name;
    this.colors = ["red","blue","green"];
    }
    SuperType.prototype.sayName = function() {
    console.log(this.name);
    }
    function SubType(name, age) {
    SuperType.call(this,name);//继承属性
    this.age = age;
    }
    //继承方法
    SubType.prototype = new SuperType();
    Subtype.prototype.constructor = Subtype;
    Subtype.prototype.sayAge = function() {
    console.log(this.age);
    }
    var instance1 = new SubType("EvanChen",18);
    instance1.colors.push("black");
    consol.log(instance1.colors);//"red","blue","green","black"
    instance1.sayName();//"EvanChen"
    instance1.sayAge();//18
    var instance2 = new SubType("EvanChen666",20);
    console.log(instance2.colors);//"red","blue","green"
    instance2.sayName();//"EvanChen666"
    instance2.sayAge();//20

    4 原型式继承

    var person = {
    name:"EvanChen",
    friends:["Shelby","Court","Van"];
    };
    var anotherPerson = Object.create(person);
    anotherPerson.name = "Greg";
    anotherPerson.friends.push("Rob");
    var yetAnotherPerson = Object.create(person);
    yetAnotherPerson.name = "Linda";
    yetAnotherPerson.friends.push("Barbie");
    console.log(person.friends);//"Shelby","Court","Van","Rob","Barbie"

    5 寄生式继承

    function createAnother(original) {
    var clone = Object.create(original);
    clone.sayHi = function () {
    alert("hi");
    };
    return clone;
    }
    var person = {
    name:"EvanChen",
    friends:["Shelby","Court","Van"]
    };
    var anotherPerson = createAnother(person);
    anotherPerson.sayHi();///"hi"

    6 寄生组合式继承

    function SuperType(name){
    this.name = name;
    this.colors = ["red","blue","green"];
    }
    SuperType.prototype.sayName = function (){
    alert(this.name);
    };
    function SubType(name,age){
    SuperType.call(this,name);
    this.age = age;
    }
    inheritProperty(SubType,SuperType);
    SubType.prototype.sayAge = function() {
    alert(this.age);
    }
  • 相关阅读:
    BZOJ2563 阿狸和桃子的游戏
    BZOJ2460 Beijing2011元素(线性基+贪心)
    BZOJ2458 Beijing2011最小三角形(分治)
    BZOJ2442 Usaco2011 Open修剪草坪(动态规划+单调队列)
    Luogu2257 YY的GCD/BZOJ2818 Gcd加强版(莫比乌斯反演+线性筛)
    BZOJ2428 HAOI2006均分数据(模拟退火)
    BZOJ2440 中山市选2011完全平方数(容斥原理+莫比乌斯函数)
    洛谷 P1783 海滩防御 解题报告
    洛谷 P2431 正妹吃月饼 解题报告
    洛谷 P2751 [USACO4.2]工序安排Job Processing 解题报告
  • 原文地址:https://www.cnblogs.com/love-yangerlei/p/8252076.html
Copyright © 2020-2023  润新知