JS深度克隆变量

来源:互联网 发布:js获取select所有的值 编辑:程序博客网 时间:2024/05/29 18:17
var obj = {name:'obj',age:21,box:{name:'box1',size:[22,23],color:['red',{name:'blue',values:'#999'}]},eat:function(){console.log('eating');}};//target深度克隆orignfunction deepClone(orign,target){var tar = target || {};var arrStr = "[object Array]";for(var prop in orign){if(orign.hasOwnProperty(prop)){if(typeof(orign[prop])=='object'&&typeof(orign[prop])!=='null'){tar[prop]=Object.prototype.toString.call(orign[prop])==arrStr?[]:{};deepClone(orign[prop],tar[prop]);}else{tar[prop] = orign[prop];}}}return tar;}var obj1 = deepClone(obj);

原创粉丝点击