码迷,mamicode.com
首页 > 其他好文 > 详细

第六章:继承

时间:2016-04-05 19:21:46      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

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;

}

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实现类式继承

function Child(){

  Parent.apply(this,arguments)       

}

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

 

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

 

第六章:继承

标签:

原文地址:http://www.cnblogs.com/joya0411/p/5356160.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!