See this article on when To and Not To use the delete
operator.
This does not appear to be a proper use.
Local variables cannot be deleted as they are marked internally with the DontDelete
attribute. There are occasions when you might want to clear a local variable (if you want to release any memory used by it and the scope may survive indefinitely in a closure), but you don't use the delete
operator for this purpose - you can just set it to null
.
In normal functions that don't create closures, any local variables will simply be garbage collected when the function completes and if there are no other references to their data in other code, that data will be freed by the garbage collector.
The only time you need to worry about clearing references to data is when you have a scope that will exist for a long duration of time (closure or global) and you no longer need that data and it's useful to free up its memory usage.
FYI, the most common use of the delete
operator is to remove a property from an object as in:
var foo = {x: 1, y: 2};
delete foo.x;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…