¿Por qué no se permite eliminar en el modo estricto Javascript5?


Soy bastante nuevo en javascript, pero estoy enamorado de su expresividad peligrosamente rápida y suelta. Dicho esto, me di cuenta de que aparentemente cuando se opera en el modo "use strict", no se pueden eliminar objetos. No soy un gran fan de eliminar cosas (ya que, en teoría, scope debería encargarse de eso de todos modos), pero me pregunto cuál fue la motivación detrás de eliminar esta característica.

Author: garg10may, 2013-05-20

2 answers

La instrucción delete todavía está permitida en modo estricto, pero algunos usos particulares de ella son erróneos. Solo está permitido para propiedades de objetos, no nombres simples, y solo para propiedades de objetos que se pueden eliminar.

Así

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

Está bien, pero

delete Object.prototype;

No Es, y tampoco es

delete a;

(Este último es en realidad un error de nivel de sintaxis, mientras que un intento de eliminar una propiedad undeletable es un error de tiempo de ejecución.)

 64
Author: Pointy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-05-20 15:13:12

[suprimir] Explicado en detalle con el ejemplo

// 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();
 2
Author: Amit Shah,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-07 07:45:28