Deleting a window property in IE

JavascriptInternet ExplorerPropertiesWindow

Javascript Problem Overview


I can't find any information on this issue; why doesn't the following code work in IE?

window.x = 45;
delete window.x;
// or delete window['x'];

IE reports an "object doesn't support this action" error. Does it have anything to do with that iterating over window properties in IE issue?

Javascript Solutions


Solution 1 - Javascript

Gasper made a comment with the solution he finished on, but I think it's worth calling out as an actual answer:

try { 
    delete window.x; 
} catch(e) { 
    window["x"] = undefined; 
}

Interesting issue, I was just banging my head against it tonight. The exception is thrown on IE but not Firefox. I would suspect this workaround leaks memory, so use it sparingly.

It was asked, why not just assign undefined? It matters if you want to enumerate the keys later (though if you're relying on the workaround, the key enumeration still won't do what you want...). But anyhow, to highlight the difference between delete and simply assigning undefined (http://jsfiddle.net/fschwiet/T4akL/):

var deleted = {
    a: 1
};

var cleared = {
    a: 1
};

delete deleted["a"];
cleared["a"] = undefined;

for(var key in deleted) {
    console.log("deleted has key", key);
}

for(var key in cleared) {
    console.log("cleared has key", key);
}

console.log("deleted has a?", deleted.hasOwnProperty('a'));
console.log("cleared has a?", cleared.hasOwnProperty('a'));

Produces output:

cleared has key a
deleted has a? false
cleared has a? true 

Solution 2 - Javascript

I would do it this way:

    window[x] = undefined;
    try{
        delete window[x];
    }catch(e){}

Solution 3 - Javascript

Does this help?

window.x = 45;
alert(window.x);
window.x = null;

I tried this in IE and window.x did have a value, which proves it can be set. Setting the value to null is your best bet for clearing it out.

Solution 4 - Javascript

I implemented this solution when dealing with caching my own data - the data wasn't much the the frequency of the cache was such that the memory leak might have become an issue. It's costly, but periodically remapping the object was the easiest way for me to be sure it wasn't getting out of hand.

obj = {a: 1, b: 2, c: 3};
var max;

function unset(obj, key) {
    try {
        delete obj[key];
    } catch (e) {
        obj[key] = undefined;
    }

    max++;

    if(max > 200) {
        var keys = Object.keys(obj);
        var len = keys.length;
        var n_obj = {};
        for(var i = 0; i < len; i++) {
            if(obj.hasOwnProperty(keys[i]) && obj[keys[i]] !== undefined) {
                n_obj[keys[i]] = obj[keys[i]];
            }
        }
        return n_obj;
    }
    return obj;
}

obj; //{a: 1, b: 2, c: 3}
obj = unset(obj, "b"); //{a: 1, b: undefined, c: 3} OR {a: 1, c: 3}
//and then eventually we'll garbage collect and...
obj = unset(obj, "b"); //{a: 1, c: 3}   

Hopefully, that's useful to some!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiongasper_kView Question on Stackoverflow
Solution 1 - JavascriptFrank SchwietermanView Answer on Stackoverflow
Solution 2 - Javascriptuser155314View Answer on Stackoverflow
Solution 3 - JavascriptFentonView Answer on Stackoverflow
Solution 4 - JavascriptMikebert4View Answer on Stackoverflow