Closed dijs closed 11 years ago
I'm a little torn on this one...
JavaScript's delete
is quirky and sometimes confusing. For example:
x = function () {return 2};
delete x();
Doesn't throw any errors or indicate any issues (except in strict mode, which Kal doesn't use at this time).
var y = 2;
delete y;
Just does nothing.
Is there something better we can do?
The only reason you should use delete if for object properties, not variables or functions.
For the problem I had, I solved it without using the delete operator by assigning it a value of 'undefined'
There is no equivalent to the delete
keyword. The best option is to warn people in the documentation to be careful.
A couple options would be:
delete
isn't supporteddelete
(really easy)delete
, but do compile-time checks to make sure they aren't trying to delete variables and are only deleting a property or array index.I'm ok with number 1 if nobody can think of a use case that really benefits from delete
.
I don't like number 2 because I think it's confusing to anyone who isn't already a JavaScript expert.
Number three isn't bad, but I worry people will try things like:
x = [1,2,3]
delete x[1]
print x.length
and expect to get 2
.
However, I don't think
x = {a:1,b:2}
delete x.a
print x.a
Is confusing.
For number 4, the best idea I have is:
delete property a from x
delete item 3 from y
delete items 10 to 20 from z
In this case I'd use splice
rather than delete
for the arrays. I think this would prevent people from even trying things like delete a
.
But I'm looking for thoughts.
4 is sick! That would be awesome syntax to have.
Sounds like 4 would encourage the correct usage.
Cannot use "delete" to remove object properties...