Check Javascript Removal Support

In Javascript, you can delete an object property:

 var o = { x: 1, y: 2 }; var wasDeleted = delete ox; 

Now ox should be undefined and wasDeleted equal to true .

However, you can only delete your own objects, and, unfortunately, browsers have different ideas about 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 does not support this action"

Great. Please note that this does not mean that delete not supported ...

 // Oops, no var, so this is now a global, should've 'use strict' o = { x: 1, y: 2 }; // Works delete ox; // Works delete window.oy; // Fails, but only in IE6-8 :-( delete window.o 

I understand that I can add a try {...} catch , but ...

Is there a way to check if the browser supports delete specific object before calling it?

those. can I tell if the property is considered by the browser to be a host or native?

+4
source share
1 answer

delete is a core JavaScript feature that is also supported by IE6-8. It's just that these legacy browsers deal differently with removing the properties of an imutable or native / host object. I'm afraid the try-catch is your only option for this.

+3
source

Source: https://habr.com/ru/post/1487012/


All Articles