Search code examples
javascriptnavigator

Javascript: how to correctly delete property from window.navigator?


Question: how to delete appName property ( as example ) from window.navigator object?

console.log(window.navigator.hasOwnProperty("appName"));
// -> false

console.log(window.navigator.__proto__.hasOwnProperty("appName"));
// -> true

console.log(Object.getOwnPropertyDescriptor(navigator.__proto__, "appName"));
// -> {set: undefined, enumerable: true, configurable: true, get: ƒ}

Problems:

  1. there is no such function like Object.deleteProperty (but there is Object.defineProperty)
  2. i can not initialize Navigator object directly ("Illegal constructor" error)
  3. i can not create new object and just set its __proto__ property to window.navigator.__proto__ (requesting appName property of new object will result in "Illegal invocation" error).

Thanks.


Solution

  • Since the property is owned by navigator prototype you need to delete this property from prototype.

    Only tested in Chrome. This might not work in another browsers because most DOM related objects are exotic.

    console.log(window.navigator.appName)
    
    delete Object.getPrototypeOf(window.navigator).appName;
    
    console.log(window.navigator.appName)