Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for support of Javascript delete functionality

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?

like image 237
Keith Avatar asked Jun 19 '13 10:06

Keith


1 Answers

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.

like image 144
jAndy Avatar answered Sep 30 '22 06:09

jAndy