[英]Behavior of delete operator in javascript
It seems in JavaScript you can't delete
function arguments but you can delete
global variables from a function.似乎在 JavaScript 中您无法
delete
function arguments 但您可以从 ZC1C425268E687A94F1C1 中delete
全局变量
Why this behavior?为什么会有这种行为?
var y = 1;
(function (x) { return delete y; })(1); // true
(function (x) { return delete x; })(1); // false
Actually, neither should return true
, and indeed they don't in Firefox or Chrome (untested in other browsers).实际上,两者都不应该返回
true
,而且它们确实不在 Firefox 或 Chrome 中(在其他浏览器中未经测试)。 I imagine you tested this with Firebug or another browser console, which changes things due to the console using eval()
.我想您使用 Firebug 或其他浏览器控制台对此进行了测试,由于控制台使用
eval()
会改变一些事情。 delete
only deletes properties of an object and cannot normally delete a variable declared using var
, whatever the scope. delete
只会删除 object 的属性,并且不能正常删除使用var
声明的变量,无论 scope 是什么。
Here's an excellent article by Kangax on the subject: http://perfectionkills.com/understanding-delete/这是 Kangax 关于这个主题的一篇优秀文章: http://perfectionkills.com/understanding-delete/
Edit: Both return false
in normal use (ie not within the Firebug or browser console, which use eval()
).编辑:两者在正常使用中都返回
false
(即不在使用eval()
的 Firebug 或浏览器控制台中)。 See Tim Down's answer (it should be the accepted one).请参阅Tim Down 的答案(应该是公认的答案)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.