• 继承


    1、继承

      (1)传统形式:原型链

        过多的继承了没用的属性

      (2)借用构造函数

        不能继承借用构造函数的原型

        每次构造函数都要多走一个函数

      (3)共享原型

        不能随便改动自己的原型

      (4)圣杯模式

    2、继承实例

      (1)原型链继承

        Grand.prototype.lastName  =  'aa'

        function  Grand ( ){ }

        var  grand  =  new  Grand( )

        Father.prototype  =  grand

        function  Father ( ){

          this.name  =  'bb'

        }

        var  father  =  new  Father( )

        Son.prototype  =  father

        function  Son ( ){ }

        var  son  =  new  Son( )

      (2)借用构造函数继承

        function  Person(name, age, sex){

          this.name  =  name;

          this.age  =  age;

          this.sex  =  sex;

        }

        function  Student(name, age, sex, grade){

          Person.call(this, name, age, sex)

          this.grade  =  grade

        }

        var  student  =  new  Student('aa', 18,  'man', 'one' )

      (3)组合继承apply+prototype

       (4)寄生继承

        function  Cat(o){

          var  obj  =  Object.create(o)

          o.type  =  "动物"

          return  obj;

        }

        var  obj  =  {name:"tom"}

        var  c  =  Cat(obj)

      (5)共享原型继承

        Father.prototype.lastName  =  'aaa'

        function  Father( ){ }

        Son.prototype  =  Father.prototype

        function  Son( ){ }

        var  son  =  new  Son( )

      (6)圣杯模式

        function  inherit (target, origin){

          function  F ( ){ }

          F.prototype  =  Origin.prototype;

          Target.prototype  =  new  F( )

          Target.prototype.constructor  =  Target

          Target.prototype.uber  =  Origin.prototype

        }

        Father.prototype.lastName  =  'aaa'

        function  Father ( ){ }

        function  Son ( ){ }

        inherit(Son, Father)

        var  son  =  new  Son( )

        var  father  =  new  Father( )

  • 相关阅读:
    gitlab搭建
    .NET Core 跨平台物联网开发:设置委托事件(二)
    .NET Core 跨平台物联网开发:连接阿里云IOT(一)
    Orange Pi 3 GPIO 笔记
    树莓派踩坑备忘录 -- 使用 Linux
    .NET Core / C# 开发 IOT 嵌入式设备的个人见解
    阿里云 IOT 对接设备开发 C# 开发设备对接阿里云 IOT平台
    跨平台开发 -- C# 使用 C/C++ 生成的动态链接库
    .NET Core 使用 EF 出错的解决方法
    arm 开发板更新 gcc/gcc++ | Debain 更新 gcc,无需编译直接更新 gcc
  • 原文地址:https://www.cnblogs.com/cuishuangshuang/p/13275962.html
Copyright © 2020-2023  润新知