You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Object.prototype.clone=function(){// Handle null or undefined or functionif(null==this||"object"!=typeofthis)returnthis;// Handle the 3 simple types, Number and String and Booleanif(thisinstanceofNumber||thisinstanceofString||thisinstanceofBoolean)returnthis.valueOf();// Handle Dateif(thisinstanceofDate){varcopy=newDate();copy.setTime(this.getTime());returncopy;}// Handle Array or Objectif(thisinstanceofObject||thisinstanceofArray){varcopy=(thisinstanceofArray)?[]:{};for(varattrinthis){if(this.hasOwnProperty(attr))copy[attr]=this[attr]?this[attr].clone():this[attr];}returncopy;}thrownewError("Unable to clone obj! Its type isn't supported.");}
2.使用额外的工具函数实现,适用于大部分对象的深度复制(Deep Clone)。
functionclone(obj){// Handle the 3 simple types, and null or undefined or functionif(null==obj||"object"!=typeofobj)returnobj;// Handle Dateif(objinstanceofDate){varcopy=newDate();copy.setTime(obj.getTime());returncopy;}// Handle Array or Objectif(objinstanceofArray|objinstanceofObject){varcopy=(objinstanceofArray)?[]:{};for(varattrinobj){if(obj.hasOwnProperty(attr))copy[attr]=clone(obj[attr]);}returncopy;}thrownewError("Unable to clone obj! Its type isn't supported.");}
The text was updated successfully, but these errors were encountered:
1.下面的方法,是给Object的原型(prototype)添加深度复制方法(deep clone)。
2.使用额外的工具函数实现,适用于大部分对象的深度复制(Deep Clone)。
The text was updated successfully, but these errors were encountered: