Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is delete not allowed in Javascript5 strict mode?

I'm fairly new to javascript. I noticed that apparently when operating in "use strict" mode, you can't delete objects. I'm not a huge fan of deleting things (since, in theory, scope should take care of that anyway), but I wonder what was the motivation behind removing this feature?

like image 829
sircodesalot Avatar asked May 20 '13 15:05

sircodesalot


People also ask

How do you remove an object from strict mode?

In strict mode, an attempt to delete a variable will throw an error and is not allowed. The delete operator can only delete properties on an object. Object properties are "qualified" if they are configurable. Unlike what common belief suggests, the delete operator has nothing to do with directly freeing memory.

What is not allowed in strict mode?

Not Allowed in Strict Mode Objects are variables too. Deleting a variable (or object) is not allowed. Deleting a function is not allowed. For security reasons, eval() is not allowed to create variables in the scope from which it was called.

When would you not use strict mode?

If you have such an unrestrictedly typed code, that is used variables without declaring. One variable declared within some function/scope and used from somewhere else(it will be undeclared there) and you can't rewrite/change them, then you should not go for "use strict;" mode because it will break the code.

What happens strict mode?

Strict mode prohibits some syntax likely to be defined in future versions of ECMAScript. It prevents, or throws errors, when relatively “unsafe” actions are taken (such as gaining access to the global object). It disables features that are confusing or poorly thought out.


2 Answers

[delete] Explained in detail with example

// The delete statement is still allowed in strict mode, but some particular uses of it are erroneous. It's only allowed for object properties, not simple names, and only for object properties that can be deleted.    // "use strict";    // creates the property adminName on the global scope  adminName = "xyz";    // creates the property empCount on the global scope  // Since we are using var, this is marked as non-configurable. The same is true of let and const.  var empCount = 43;    EmployeeDetails = {    name: "xyz",    age: 5,    designation: "Developer"  };    // adminName is a property of the global scope.  // It can be deleted since it is created without var.  // Therefore, it is configurable.  console.log("delete adminName =", delete adminName); // returns true    // On the contrary, empCount is not configurable,  // since var was used.  console.log("delete empCount =", delete empCount); // returns false    // delete can be used to remove properties from objects  console.log("delete EmployeeDetails.name =", delete EmployeeDetails.name); // returns true    // Even when the property does not exists, it returns "true"  console.log("delete EmployeeDetails.salary =", delete EmployeeDetails.salary); // returns true    // delete does not affect built-in static properties  console.log("delete Math.PI =", delete Math.PI); // returns false    // EmployeeDetails is a property of the global scope.  // Since it defined without "var", it is marked configurable  console.log("delete EmployeeDetails =", delete EmployeeDetails); // returns true    x = 1;  var y = 2;    function f() {    var z = 44;      console.log("delete x =", delete x); // returns true    console.log("delete y =", delete y); // returns false    // delete doesn't affect local variable names    console.log("delete z =", delete z); // returns false  }    f.call();
like image 36
Amit Shah Avatar answered Sep 27 '22 23:09

Amit Shah


The delete statement is still allowed in strict mode, but some particular uses of it are erroneous. It's only allowed for object properties, not simple names, and only for object properties that can be deleted.

Thus

var a = {x: 0}; delete a.x; 

is fine, but

delete Object.prototype; 

is not, and neither is

delete a; 

(The latter is actually a syntax-level error, while an attempt to delete an undeletable property is a runtime error.)

like image 151
Pointy Avatar answered Sep 28 '22 00:09

Pointy