delete
operator with computed key expressions JS-0320 16 for(const board of boards) {
17 if(!(watched[board] instanceof Array)) {
18 console.error(`Invalid data for board ${board}: expected Array object, deleting.`);
19 delete watched[board]; 20 continue;
21 }
22 $(document).trigger("beginNewPostsCheck");
Deleting dynamically computed keys can be dangerous and in some cases not well optimized.
Using the delete
operator on keys that aren't runtime constants could be a sign that you're using the wrong data structures.
Using Objects
with added and removed keys can cause occasional edge case bugs, such as if a key is named "hasOwnProperty"
.
Consider using a Map
or Set
if you’re storing collections of objects.
// Can be replaced with the constant equivalents, such as container.aaa
delete container['aaa'];
delete container['Infinity'];
// Dynamic, difficult-to-reason-about lookups
const name = 'name';
delete container[name];
delete container[name.toUpperCase()];
const container: { [i: string]: number } = {
/* ... */
};
// Constant runtime lookups by string index
delete container.aaa;
// Constants that must be accessed by []
delete container[7];
delete container['-Infinity'];