javascript 为什么在 Javascript5 严格模式下不允许删除?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16652589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:31:14  来源:igfitidea点击:

Why is delete not allowed in Javascript5 strict mode?

javascriptecmascript-5strict-mode

提问by sircodesalot

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?

我对 javascript 还很陌生。我注意到显然在“使用严格”模式下操作时,您无法删除对象。我不是删除东西的忠实粉丝(因为从理论上讲,无论如何,范围都应该照顾到这一点),但我想知道删除此功能背后的动机是什么?

回答by Pointy

The deletestatement 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.

delete语句在严格模式下仍然允许,但它的某些特定用法是错误的。它只允许用于对象属性,而不是简单名称,并且只允许用于可以删除的对象属性。

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.)

(后者实际上是语法级错误,而尝试删除不可删除的属性则是运行时错误。)

回答by Amit Shah

[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();