克隆函数

<script>
		var a={
			b : 1,
			c : "abc",
			e:[1,2,3]
		},

	clone=function(obj){
		var c, i;
		if(obj.constructor==Array){
			c=[];
			for(i=0;i<obj.length;i++){
				c[i]=obj[i];
			}
			return c;
		}

		if(obj.constructor==Object){
			c=[];
			for(i in obj){
				c[i]=obj[i];
			}
			return c;
		}

		return alter("is not obj");
	},

	c={};

	for(i in a){

		if(a[i].constructor==Array || a[i].constructor==Object){

			c[i]=clone(a[i]);
		}

		else{

			c[i]=a[i];
		}
	}

	console.log(a);
	console.log(c);


</script>

 

posted @ 2015-03-14 11:28  唸随爱  阅读(203)  评论(0编辑  收藏  举报