JavaScript 对象的 DontDelete 属性

发布于 2024-11-07 07:23:33 字数 359 浏览 4 评论 0原文

根据 EcmaScript 规范,由于 DontDelete 内部参数,某些对象属性无法删除。例如:

var y = 5

不应被删除。但据我所知——确实如此。

这是 Mozilla 开发者中心的链接: https://developer.mozilla.org/en/JavaScript/Reference/Operators /Special/delete

有什么想法为什么这不能正常工作吗?

According to EcmaScript specification some objects properties cannot be deleted due to the DontDelete internal parameter. For example :

var y = 5

should not be deletable. But from what I was able to check - it is.

Here's a link at Mozilla Developer Center :
https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special/delete

Any ideas why this isn't working as it should ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

好菇凉咱不稀罕他 2024-11-14 07:23:33

有时你必须检查你所读的内容。 ECMA 规范(262,第 5 版)中没有 DontDelete 内部参数。也许 [Configurable] 属性是什么意思? delete 运算符不适用于变量或函数,它适用于对象属性:

var y=5, 
    z = {y:5};
delete y;
delete z.y;
alert(y);   //=> 5
alert(z.y); //=> undefined

从我的回答来看,这个 SO 问题出现了,TJ Crowder 给出了很好的答案。

Sometimes you have to check what you read. There is no DontDelete internal parameter in the ECMA-specification (262, ed 5). Maybe the [Configurable] property is meant? The delete operator doesn't work on variables or functions, it works on object properties:

var y=5, 
    z = {y:5};
delete y;
delete z.y;
alert(y);   //=> 5
alert(z.y); //=> undefined

From my answer, this SO question emerged, and an excellent answer from T.J. Crowder.

緦唸λ蓇 2024-11-14 07:23:33

根据 ES5 表 17:

CreateMutableBinding(N, D)
在中创建一个新的可变绑定
环境记录。字符串值 N
是绑定名称的文本。如果
可选布尔参数 D 为 true
绑定可能随后
已删除。

以及 10.5 声明绑定实例化

  1. 对于代码中的每个 VariableDeclaration 和 VariableDeclarationNoIn d,
    源文本顺序
    [...]
    二.调用 env 的 SetMutableBinding
    具体方法传递 dn,未定义,
    并严格作为论据。

这对我来说声明的变量不应该是可删除的。在全局代码中,全局对象是激活对象,即变量 obejct,因此声明的全局变量不应被删除。当然,浏览器可能不遵守这一点......

According to ES5 table 17:

CreateMutableBinding(N, D)
Create a new mutable binding in an
environment record. The String value N
is the text of the bound name. If the
optional Boolean argument D is true
the binding is may be subsequently
deleted.

and in 10.5 Declaration Binding Instantiation

  1. For each VariableDeclaration and VariableDeclarationNoIn d in code, in
    source text order do
    [...]
    ii. Call env’s SetMutableBinding
    concrete method passing dn, undefined,
    and strict as the arguments.

Which says to me that declared variables should be not deleteable. In global code, the global object is the activation object which is the variable obejct, so declared globals shouldn't be deletable. Of course, browsers may not adhere to that...

七度光 2024-11-14 07:23:33
var y = 5
alert(delete (y));

显示false。那么就无法删除了。

var y = 5
alert(delete (y));

Show false. Then, can't be deleted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文