zoukankan      html  css  js  c++  java
  • 继承的总结

    1继承的总结

    1原型继承
    function Super(name) {
    this.name = name;
    this.su = ['2', '4', '5'];
    }
    Super.prototype.sayName = function () {
    };
    function Sub() {

    }

    Sub.prototype = new Super();
    var n = new Super('xiao');
    var p = new Sub();
    p.sayName();//继承了构造函数的属性和方法,和原型的方法
    n.su.push(6);
    n.su// '2', '4', '5',‘6’
    p.su// '2', '4', '5',‘6’
    缺点构造函数的引用类型都已经复用
    2构造函数的继承
    function Super() {

    }
    function Sub() {
    Super.call(this, arguments);
    }
    var s = new Super();
    var sub = new Sub();
    解决的问题为了解决引用的不用复用
    缺点不能继承原型的类型
    3组合继承
    function Super(name) {
    this.name = name;
    this.su = ['2', '4', '5'];
    }
    Super.prototype.sayName = function () {
    };
    function Sub() {
    Super.apply(this, arguments);
    }

    Sub.prototype = new Super();
    Sub.prototype.constructor = Sub;
    var n = new Super('xiao');
    var p = new Sub();
    p.sayName();//继承了构造函数的属性和方法,和原型的方法
    n.su.push(6);
    n.su// '2', '4', '5',‘6’
    p.su// '2', '4', '5',‘6’


    4寄生式继承
    function anotherCreat(obj) {
    var clone = object(obj);
    clone.say = function () {
    alert('hi');
    }
    return clone;
    }
    var obj = {
    name: 'a',
    atrr: '3'
    }
    var a = anotherCreat(obj)
    a.say()//

    5寄生组合继承
    // 这个是终端的继承
    function Super() {

    }
    Super.prototype.sayAge = function () {

    };
    function Sub() {
    Super.call(this);
    }
    inherits();
    function inherits(Sub, Super) {
    prototype = object(Super.prototype)
    prototype.constructor = Sub;
    Sub.prototype = prototype;
    }

    var a = new Super();
    var b = new Sub();
    Sub.prototype.say = function () {

    }

    6es6 的继承
    class A{}
    class B{}
    class A extends B{
    constructor(){
    super();
    }
    }
  • 相关阅读:
    Web中Servlet简单总结
    JavaSE进阶的面试题
    多线程简单总结
    Java基础集合简单总结
    内部类和Lambda
    多态
    接口
    继承
    uni-app 中uCharts
    vue 组件传值
  • 原文地址:https://www.cnblogs.com/yayaxuping/p/9587863.html
Copyright © 2011-2022 走看看