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();
    }
    }
  • 相关阅读:
    yocto/bitbake 学习资源
    QEMU/KVM学习资源
    ubuntu 中创建和删除用户
    git 重命名本地和远程分支
    Ubuntu 上搭建 FTP 服务器
    gdb 常见用法
    git log 显示与特定文件相关的 commit 信息
    基于 qemu system mode 运行 arm 程序
    基于 qemu user mode 运行 aarch64 程序
    checking in(airport)
  • 原文地址:https://www.cnblogs.com/yayaxuping/p/9587863.html
Copyright © 2011-2022 走看看