In Javascript you can delete
an object property:
var o = { x: 1, y: 2 };
var wasDeleted = delete o.x;
Now o.x
should be undefined
and wasDeleted
is true
.
However you can only delete native objects, and unfortunately the browsers seem to have different ideas on this:
window.x = 1;
delete window.x;
Now in Chrome and IE9-10 x
will be undefined
, but in IE6-8 this throws an exception:
"Object doesn't support this action"
Great. Note that this isn't that delete
is unsupported...
// Oops, no var, so this is now a global, should've 'use strict'
o = { x: 1, y: 2 };
// Works
delete o.x;
// Works
delete window.o.y;
// Fails, but only in IE6-8 :-(
delete window.o
I realise that I can add a try {...} catch
block, but...
Is there any way to check whether a browser supports delete
against a particular object before it is called?
I.e. can I tell whether a property is considered host or native by the browser?
delete
is a basic javascript language feature which also is supported by IE6-8. It's just that these legacy browsers deal differently on deleting imutable or native / host object properties. I'm afraid a try-catch
statement is your only option to cover this.
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