Just wondering about this one :
Whats the difference, or is there a difference at all, between :
delete obj.someProperty
and
obj.someProperty=undefined
The second version sets the property to the existing value undefined
while the first removes the key from the object. The difference can be seen when iterating over the object or using the in
keyword.
var obj = {prop: 1};
'prop' in obj; // true
obj.prop = undefined;
'prop' in obj; // true, it's there with the value of undefined
delete obj.prop;
'prop' in obj; // false
The difference will be realized when iterating over the object. When deleting the property, it will not be included in the loop, whereas just changing the value to undefined will include it. The length of the object or number of iterations will be different.
Here is some great (albeit advanced) information on deleting in JavaScript:
http://perfectionkills.com/understanding-delete/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With