function clone(obj){ if(obj == null || typeof(obj) != 'object') return obj; var temp = new obj.constructor(); for(var key in obj) temp[key] = clone(obj[key]); return temp; }
Object.prototype.obj = {valeur : "obj"}; {test : "1"}.obj.obj.obj.obj.obj.obj.valeur == "obj"
function clone(obj, maxDepth) { if(obj == null || typeof(obj) != 'object') return obj; var temp = new obj.constructor(); for(var key in obj) { if (maxDepth > 1) temp[key] = clone(obj[key], maxDepth - 1); else temp[key] = obj[key]; } return temp; }
function clone(obj){ if(obj == null || typeof(obj) != 'object') return obj; var objPrototype = Object.prototype; var temp = new obj.constructor(); for(var key in obj) { if (!objPrototype[key]) temp[key] = clone(obj[key]); } return temp; }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionfunction clone(obj){ if(obj == null || typeof(obj) != 'object') return obj; var objPrototype = Object.prototype; var temp = new obj.constructor(); for(var key in obj) { if (typeof objPrototype[key] == "undefined") temp[key] = clone(obj[key]); } return temp; }