2

Read this question: How do I remove a property from a JavaScript object?

However my code uses 'use strict'; in global declaration which means it is present in the whole file.

delete statement is forbidden in strict mode (has no effect). Documentation

How can one delete properties from objects when using strict mode without having to resort to cloning and looping over properties, skipping the one that is to be deleted?

Update and clarification:

I need to delete a property from an object before sending it to the server, which complains for unknown properties.

Community
  • 1
  • 1
Matej
  • 9,548
  • 8
  • 49
  • 66
  • Your question seems to be founded on a false premise (i.e., that `delete` statements are forbidden in strict mode). What are you *actually* trying to do? What is problem are you *actually* observing? A small, complete code sample that reproduces your issue would be helpful. – apsillers Aug 29 '14 at 16:25
  • Where does that say that the `delete` statement was forbidden or had no effect? – Bergi Aug 29 '14 at 16:25
  • @Bergi read the documentation. `Third, strict mode forbids deleting plain names. delete name in strict mode is a syntax error:` – Matej Aug 29 '14 at 16:26
  • But a property isn't a plain name. – Barmar Aug 29 '14 at 16:26
  • 1
    http://stackoverflow.com/questions/16652589/why-is-delete-not-allowed-in-javascript5-strict-mode see the answer – dave Aug 29 '14 at 16:26
  • Yes, things like `with(obj) delete x;` or `window.x = …; delete x` are forbidden. You can still remove properties: `delete obj.x` or `delete window.x`. – Bergi Aug 29 '14 at 16:27

1 Answers1

8

delete is not forbidden in strict mode.

  1. Deleting un-deletable properties in strict mode throws an error. In non-strict mode it fails silently. Either way, it's not possible except for cloning the object.

  2. You can't delete plain names; a syntax error is thrown. If the variable is a global variable, you can get around it like so;

    // Imagine we're global here.
    var foo = 4;
    
    delete foo; // syntax error
    delete window.foo; // works.
    
Matt
  • 74,352
  • 26
  • 153
  • 180