为什么在Javascript5严格模式下不允许删除? [英] Why is delete not allowed in Javascript5 strict mode?

查看:99
本文介绍了为什么在Javascript5严格模式下不允许删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对javascript很新,但我很喜欢它快速而又松散的表现力。也就是说,我注意到显然在使用严格模式下操作时,您无法删除对象。我不是很喜欢删除东西(因为理论上,范围应该照顾到这一点),但是我想知道删除这个功能背后的动机是什么?

I'm fairly new to javascript, but I'm in love with it's dangerously fast and loose expressiveness. That said, 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?

推荐答案

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

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.

因此

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

没问题,但是

delete Object.prototype;

不是,也不是

delete a;

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

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

这篇关于为什么在Javascript5严格模式下不允许删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆