zoukankan      html  css  js  c++  java
  • 继承

    继承是类和类之间的关系
    继承可以使得子类别具有父类别的各种属性和方法
    原型链继承
    a.valueOf() 面向对象叫成员属性,js叫原型链继承
    Object和Array两个类之间没有任何关系
    但是Object.prototype和Array.prototype有一个重要的关系,所有的对象都是从new Object()构造出来的,Array.prototype也是new Object()构造出来的
    Array.prototype.proto===Object.prototype
    js 子类函数 Array 父类函数 Object
    继承:子类构造出来的对象直接拥有父类的属性,他的实质就是两次的原型搜索

    1
    2
    3
    a=new Array() 
    a.push() push是实例属性 他的类是Array
    a.valueOf()继承自Object

    在原型链上一次查找不叫继承,两次才叫继承

    类就是能产生对象的东西

    只有构造函数才有prototype属性的,prototype属性只有一个功能,存放共有属性的地址
    继承的两种写法

    1.ES 5 写法
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    function Human(name){  //function Human(){}就是类
    this.name = name
    }
    Human.prototype.run = function(){
    console.log("我叫"+this.name+",我在跑")
    return undefined
    }
    function Man(name){
    Human.call(this, name) //Man继承Human
    this.gender = '男'
    }

    Man.prototype.__proto__=Human.prototype //IE不支持

    var f = function(){}
    f.prototype = Human.prototype //只要Human的prototype
    大专栏  继承s="line"> Man.prototype = new f() //IE支持,f就是去掉‘this.name=name’的Human

    Man.prototype.fight = function(){
    console.log('糊你熊脸')
    }

    IE不能操作prototype,但是new可以操作prototype

    var obj = new Fn()
    new会做五个事情

    1. 产生一个空对象
    2. this=空对象
    3. this.proto=Fn.prototype //this===obj.prototype
    4. 执行Fn.call(this,x,y,….)
    5. return 第四步的结果

    2.ES 6 写法 MDN链接

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    class Human{
    constructor(name){
    this.name = name //自身属性
    }
    run(){
    console.log("我叫"+this.name+",我在跑")
    return undefined
    }
    }
    class Man extends Human{ //Man extends Human===Man.prototype.__proto__=Human.prototype
    constructor(name){
    super(name) //===Human.call(this, name)
    this.gender = '男'
    }
    get s(){
    return '人类' //生成非函数的属性 s:'人类'
    }
    fight(){
    console.log('糊你熊脸')
    }
    }

    两个方法的优劣

    1. ES5的比较繁琐,但是容易理解
    2. ES6不容易理解,不能在prototype上直接生成非函数的属性
  • 相关阅读:
    DockerFile体系结构
    Nignx(二) server_name 规则
    解决Redis分布式锁——死锁问题
    redis缓存穿透,缓存击穿,缓存雪崩原因+解决方案
    Docker_Linux
    正则例子
    部属流程
    Mysql insert语句的优化
    MySQL innodb_fast_shutdown参数讲解
    MySQL技术内幕InnoDB存储引擎(表&索引算法和锁)
  • 原文地址:https://www.cnblogs.com/lijianming180/p/12268034.html
Copyright © 2011-2022 走看看