Created
November 29, 2018 12:57
-
-
Save scorsi/1c9ca7220666ef93e81a1ea53faa778a to your computer and use it in GitHub Desktop.
Clone object JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function clone(obj) { | |
if (obj === null || typeof obj === 'undefined' || typeof obj !== 'object') return obj; | |
cloned = {}; | |
for (key in obj) { | |
if (Object.hasOwnProperty.call(obj, key)) { | |
cloned[key] = clone(obj[key]); | |
} | |
} | |
return cloned; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var nbError = 0; | |
var nbSuccess = 0; | |
function assert(message, bool) { | |
if (bool === true) { | |
nbSuccess++; | |
console.log(message + ': OK'); | |
} | |
else { | |
nbError++; | |
console.log(message + ': ASSERTION ERROR'); | |
} | |
} | |
function displayTestingResult() { | |
console.log('There are ' + nbError + ' failed test.'); | |
console.log('There are ' + nbSuccess + ' successful test.'); | |
} | |
function testingWithoutClone(obj) { | |
var notClonedObj = obj; | |
console.log('obj:', obj); | |
console.log('notClonedObj:', notClonedObj); | |
console.log('----------------------------------------------'); | |
assert('obj and notClonedObj should be equal to itself...', obj === notClonedObj); | |
notClonedObj.unknownParam = 0.0; | |
assert('Modify notClonedObj should modify obj', typeof obj.unknownParam === 'number'); | |
console.log('----------------------------------------------'); | |
console.log('obj:', obj); | |
console.log('clonedObj:', notClonedObj); | |
} | |
function testingWithClone(obj) { | |
var clonedObj = clone(obj); | |
console.log('obj:', obj); | |
console.log('clonedObj:', clonedObj); | |
console.log('----------------------------------------------'); | |
assert('obj and clonedObj should not be equal to itself', obj !== clonedObj); | |
clonedObj.unknownParam = 0.0; | |
assert('Modify clonedObj should not modify obj', typeof obj.unknownParam === 'undefined'); | |
console.log('----------------------------------------------'); | |
console.log('obj:', obj); | |
console.log('clonedObj:', clonedObj); | |
} | |
console.log('Testing basic object...'); | |
console.log('==='); | |
console.log('=== Without cloning'); | |
console.log('==='); | |
testingWithoutClone({param1: '1'}); | |
console.log('==='); | |
console.log('=== With cloning'); | |
console.log('==='); | |
testingWithClone({param1: '1'}); | |
console.log(); | |
console.log('========================================================================'); | |
console.log(); | |
console.log('Testing custom object (without functions)...'); | |
var MyObjectWithoutFunction = function () { | |
this.param1 = '1'; | |
this.param2 = 2; | |
}; | |
console.log(); | |
console.log('==='); | |
console.log('=== Without cloning'); | |
console.log('==='); | |
console.log(); | |
testingWithoutClone(new MyObjectWithoutFunction()); | |
console.log(); | |
console.log('==='); | |
console.log('=== With cloning'); | |
console.log('==='); | |
console.log(); | |
testingWithClone(new MyObjectWithoutFunction()); | |
console.log(); | |
console.log('========================================================================'); | |
console.log(); | |
console.log('Testing custom object (with functions)...'); | |
var MyObjectWithFunction = function () { | |
this.param1 = '1'; | |
this.param2 = 2; | |
this.function1 = function () { | |
}; | |
}; | |
console.log(); | |
console.log('==='); | |
console.log('=== Without cloning'); | |
console.log('==='); | |
console.log(); | |
testingWithoutClone(new MyObjectWithFunction()); | |
console.log(); | |
console.log('==='); | |
console.log('=== With cloning'); | |
console.log('==='); | |
console.log(); | |
testingWithClone(new MyObjectWithFunction()); | |
console.log(); | |
console.log('========================================================================'); | |
console.log(); | |
displayTestingResult(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment