zoukankan      html  css  js  c++  java
  • 继承2

    1、构造器工作模式——原型链法:

      Child.prototype = new Parent();   //Child.prototype.constructor改变了,成为Parent

      Child.prototype.constructor = Child;

    2、构造器工作模式——仅从原型链继承法(速度越快,不需要新建对象实例,不存在链,缺点:子对象修改会影响到父对象,此方法不好,最好不要使用)

      Child.prototype = Parent.prototype;

      Child.prototype.constructor = Child;

    3、构造器工作模式——临时构造器法(利用空函数来原型继承,父对象不会受到子对象的影响

      function exend(Child,Parent){

        var F = function(){};

        F.prototype = Parent.prototype;

        Child.prototype = new F();

        Child.prototype.constructor = Child;

            child.uber = Parent.prototype;   //可以找到父对象                     =〉 uber在constructor下,即xxx.constructor.uber访问

      }

    4、构造器工作模式——原型属性copy法(注意这里不支持引用类型的copy)

      function extend2(Child,Parent){

        var p = Parent.prototype,

          c = Child.prototype;

        for (var i in p){

          c[i] = p [i]

        }

        c.uber = p;                     =>  uber在__proto__下面,即XXX.uber访问

      }

    5、对象工作模式——全属性copy法(注意这里不支持引用类型的copy)

      function extendCopy(p){

        var c = {};

        for (var i in p){

          c[i] = p[i];

        }

        c.uber = p;         =>uber就在对象下面,即XXX.uber即可

        return c;

      }

    6、对象工作模式——深拷贝法(此方法建议使用hasOwnProperty不会误copy不需要的继承属性,还需要判断是否是对象,若为对象需要进行递归)

      function deepCopy(p,c){
        var c = c || {};
        for (var i in p){
          if (p.hasOwnProperty(i)){

            if (typeof p[i] ==='object'){

              c[i] = Array.isArray(p[i]) ? [] : {};

              deepCopy(p[i],c[i]);

            } else {
              c[i] = p[i];
            }
          }
        }

        c.uber = p;
        return c;

      }

      如何不支持Array.isArray()函数,可以如下实现

      if ( typeof Array.isArray != 'function' ){

        Array.isArray = function(value){

          return Object.prototype.toString.call(value) == '[object Array]'

        }

      }

    7、对象工作模式——原型继承法(道格拉斯.克罗克福德提供的方法,感觉跟3很像,使用原型链模式)

     function object(o){

      var n,

        F = function(){};

      F.prototype = o;

      n = new F();

      n.uber = o;

      return n;

    }

    Object.create()只有一个参数的时候,跟上面object函数相同

    没有必要兴师动众创建构造函数,只想让一个对象与另一个对象类似,可以使用此方式,但这里要注意引用类型,他的属性会共享相应的值

    8、对象工作模式——扩展与增强模式(方法7+方法5的结合)

    function objectPlus(o,stuff){

      var n,

        F = new function(){};

      F.prototype = o;

      n = new F();

      n.uber = o;

      for ( var i in stuff ){

        n[i] = stuff[i]

      }

      return n;

    }

    9、对象工作模式——多重继承法(也类似copy继承的形式)

    function multi(){

      var n,

        stuff,

        j = 0,

        len = arguments.length;

      for (var j=0,j<len,j++){

        stuff = anguments[j];

        for (var i in stuff){

          if ( stuff.hasOwnProperty(i)){

            n[i] = stuff[i];

          }      

        }

      }

      return n;

    }

    10、对象工作模式——寄生继承法(调用方法7,原型继承法,使用原型链模式)

    function parasite(victim){

      var that = object(victim); 

      that.more = 1;

      return that;

    }

    11、构造器工作模式——构造器借用法(可以结合方法1使用)

    function Child(){

      Parent.apply(this,arguments)       //自身属性的继承(第二次)

    }

    Child.prototype = new Parent();          //原型的继承(第一次)

    Child.prototype.constructor = Child;

    此方法效率较低些,会调用两次超类型构造函数,第一次创建子类型原型的时候,第二次子类型构造函数内部

    12、构造器工作模式——构造器借用和属性copy法(结合方法4原型属性copy法)

    function Child(){

      Parent.apply(this,arguments)       

    }

    extend2(Child,Parent)

    13、构造器工作模式——使用Object.create实现类式继承(最佳方式,不会调用两次,也叫寄生组合式继承)

    方法一:使用 Object.create

    function Child(){

      Parent.apply(this,arguments)       

    }

    Child.prototype = Object.create(Parent.prototype)   //Object.create内部实现就是方法3

    Child.prototype.constructor = Child;

     参考:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/create

     方法二:结合3

    function Child(){

      Parent.apply(this,arguments)       

    }

    var F = function(){}

    F.prototype = Parent.prototype;

    Child.prototype = new F();

    Child.prototype.constructor = Child;

    Child.uber = Parent.prototype; // 父对象

  • 相关阅读:
    Android Studio 单刷《第一行代码》系列 05 —— Fragment 基础
    Android Studio 单刷《第一行代码》系列 04 —— Activity 相关
    Android Studio 单刷《第一行代码》系列 03 —— Activity 基础
    Android Studio 单刷《第一行代码》系列 02 —— 日志工具 LogCat
    Android Studio 单刷《第一行代码》系列 01 —— 第一战 HelloWorld
    IDEA 内网手动添加oracle,mysql等数据源,以及server returns invalid timezone错误配置
    eclipse maven设置
    IntelliJ IDE 常用配置
    eclipse maven 常见问题解决方案
    Maven 安装和配置
  • 原文地址:https://www.cnblogs.com/joya0411/p/5356160.html
Copyright © 2011-2022 走看看