zoukankan      html  css  js  c++  java
  • 继承

    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( )

  • 相关阅读:
    n!末尾有几个零
    NYOJ 14(会场安排)
    使用dynamic来简化反射实现,并且提高了性能。
    VB.NET 、Java 与 C# 语法对比。
    你不得不使用的XML代码生成器,那就是XmlFactory
    C# 和vb.net事件
    SQL Server 2008中的hierarchyid
    系统架构师基础到企业应用架构客户端/服务器
    Asp.Net在IIS上运行不了,就试下下面方法应该可以你的问题
    为你的博客添加几分色彩
  • 原文地址:https://www.cnblogs.com/cuishuangshuang/p/13275962.html
Copyright © 2011-2022 走看看