Properly clearing properties
If you want to clear a property of an object, simply setting it to undefined won’t always cut it, based on how you will access/test the property later on:
// The setup
var obj = {
prop: 1
};
// Clear property value
obj.prop = undefined;
The property is still attached to the object as a property and can be enumerated:
for (var key in obj) {
// Will iterate through "prop"
}
typeof obj.prop == "undefined" // true
obj.hasOwnProperty("prop") // true -- it's still there!
To completely eliminate a property, you need to delete it:
delete obj.prop;
for (var key in obj) {
// Won't iterate through "prop" anymore
}
typeof obj.prop == "undefined" // true
obj.hasOwnProperty("prop") // false -- it's gone completely!