So, I was writing a function that determined the differences between the starting window
, and the new window
whenever the function is called. However, nothing was logged:
var differences = (function() {
var original = {};
for (var i in window) {
if (window.hasOwnProperty(i)) {
original[i] = window[i];
}
}
// this is the area I refer to further down in my post
return function() {
// should find differences between original and new window
for (var i in window) {
if (!(original.hasOwnProperty(i)) && window.hasOwnProperty(i)) {
console.log(i + ": " + window[i]);
}
}
};
})();
var abc = 5;
differences(); // nothing is logged
So, I decided to check what abc
was in the original copy:
// after first for loop in foo differences function
console.log(original.hasOwnProperty("abc")); // true
console.log(original.abc);
// this logs that abc is defined as undefined
One would think that because differences
is defined first, that abc
shouldn't exist in the original
Object. But, since it does exist in the original
Object, why is it defined as undefined
and not 5?
But, what confuses me even more is the hasOwnProperty
line I wrote
console.log(original.hasOwnProperty("abc"));
when ran in JSFiddle it logs false
, but if I make a blank HTML file with JS, it logs true
.
Can someone explain these odd occurrences?