During my attempts to troubleshoot an issue in a JS plugin, I noticed that one variable was sometimes affecting another and sometimes not. In order to investigate further, I added the following code:
var a = $('#w3').data('fileinput').initialPreviewConfig[index];
var b = previewCache.data[id].config[index];
if (a === Object(a) && b === Object(b)) { //check a and b are objects
if(a == b && a === b) { //check they reference same object
a = null; //set a to null, so b should be null as well
if(a === null && b !== null) {
console.log("This should not be printed!"); //but it is not!
}
}
}
The output displays This should not be printed
.
I'm puzzled by why modifying one variable does not affect the other when both are pointing to the same object. Could this be due to an event or perhaps an overridden operator?
Edit:
In an attempt to simplify the code, removing variables 'a' and 'b' results in sometimes showing This should not be printed
, sometimes not.
if ($('#w3').data('fileinput').initialPreviewConfig[index] === Object($('#w3').data('fileinput').initialPreviewConfig[index]) && previewCache.data[id].config[index] === Object(previewCache.data[id].config[index])) {
if($('#w3').data('fileinput').initialPreviewConfig[index] == previewCache.data[id].config[index] && $('#w3').data('fileinput').initialPreviewConfig[index] === previewCache.data[id].config[index]) {
$('#w3').data('fileinput').initialPreviewConfig[index] = null;
if($('#w3').data('fileinput').initialPreviewConfig[index] === null && previewCache.data[id].config[index] !== null) {
console.log("This should not be printed!");
}
}
}