lodash _.clone, _.cloneDeep test
_.clone, _.cloneDeep test
HTML
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<button id='bt'>Test Start</button>
<div id='ori'></div>
<div id='clone'></div>
<div id='deepClone'></div>
JavaScript
function test() {
var oriObj = {
value: 'test1',
inObj: {
value: 'test1'
}
};
var cloneObj = _.clone(oriObj);
var cloneDeepObj = _.cloneDeep(oriObj);
function changeValue(obj, value) {
obj.value = value;
}
function printAllObject() {
$('#ori').text(oriObj.value + ',' + oriObj.inObj.value);
$('#clone').text(cloneObj.value + ',' + cloneObj.inObj.value);
$('#deepClone').text(cloneDeepObj.value + ',' + cloneDeepObj.inObj.value);
}
// test1, cloneObj value change
changeValue(cloneObj, 'normal clone test');
changeValue(cloneObj.inObj, 'normal clone test');
console.log('ori obj value > ', oriObj.value);
console.log('clone obj value > ', cloneObj.value);
// test2, cloneDeepObj value change
changeValue(cloneDeepObj, 'deep clone test');
//changeValue(cloneDeepObj.inObj, 'deep clone test');
console.log('ori obj value > ', oriObj.value);
//console.log('clone obj value > ', cloneDeepObj.value);
printAllObject();
}
$('#bt').click(test);