JS实现extend函数

 

//写一个函数,该函数的名称是extend,有两个参数:destination,source

1、如果destination和source都是json对象,完成从source到destination的复制
2、如果destination是一个函数,source是一个json对象,则把source中的每一个key,value对赋值给destination的prototype
3、如果destination,source都是函数,则把source的prototype中的内容赋值给destination的prototype


1
var extend = function(destination,source){ 2 if(typeof destination == "object"){//destination是一个json对象 3 if(typeof source == "object"){//source是一个json对象 4 //把source中的每一个key,value值赋值给destination 5 for(var i in source){ 6 destination[i] = source[i]; 7 } 8 } 9 } 10 11 if(typeof destination == "function"){ 12 if(typeof source == "object"){ 13 for(var i in source){ 14 destination.prototype[i] = source[i]; 15 } 16 } 17 if(typeof source == "function"){ 18 destination.prototype = source.prototype; 19 } 20 } 21 return destination; 22 }
原文地址:https://www.cnblogs.com/Magiccwl/p/7295895.html