search

Home  >  Q&A  >  body text

javascript - Variable delete declared through var cannot be deleted, but it is also a property of window?

The variable delete declared through var cannot be deleted, but it is also a property of window? Seek to explain

var foo = 1;
window.bar = 2;
delete foo;
delete bar;
console.log(window.foo,window.bar)//1 undefined
大家讲道理大家讲道理2726 days ago734

reply all(1)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-06-14 10:54:39

    Because the variable declared with var has the attribute configurable = false, so it cannot be deleted.

    var ss = 0;
    console.log(Object.getOwnPropertyDescriptor(window, 'ss'));
    
    /*
    {
        configurable: false
        enumerable: true
        value: 0
        writable: true
    }
    */

    reply
    0
  • Cancelreply