原型继承和拷贝继承备份

原型继承:
function extend(Child, Parent) {
    var F = function(){};
    F.prototype = Parent.prototype;
    Child.prototype = new F();
    Child.prototype.constructor = Child;
    Child.uber = Parent.prototype;
  }


浅拷贝继承:
function extendCopy(p) {
    var c = {};
    for (var i in p) { 
      c[i] = p[i];
    }
    c.uber = p;
    return c;
  }


深拷贝继承:
function deepCopy(p,c){
    var c=c||{};
    for(var i in p){
        if(typeof p[i]=="object"){
            c[i]=(p[i].constructor=="Array")?[]:{}
            deepClone(p[i],c[i])
        }else{
            c[i]=p[i]
        }
        
    }
    return c;
}

var p={"name":"leyi",child:{
    "cname":"zzn",schild:{
        "sname":"qiezijiucai"
    }
}}
var c={}
c=deepClone(p,c)
p.child.cname="halo"
console.info(p)
console.info(c)

  

posted @ 2015-08-25 11:01  韭菜茄子  阅读(191)  评论(0编辑  收藏  举报