最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Behavior of delete operator in javascript - Stack Overflow

programmeradmin1浏览0评论

It seems in JavaScript you can’t delete function arguments but you can delete global variables from a function.

Why this behavior?

var y = 1;
(function (x) { return delete y; })(1); // true

(function (x) { return delete x; })(1); // false

It seems in JavaScript you can’t delete function arguments but you can delete global variables from a function.

Why this behavior?

var y = 1;
(function (x) { return delete y; })(1); // true

(function (x) { return delete x; })(1); // false
Share Improve this question edited Aug 10, 2011 at 10:18 Mathias Bynens 150k54 gold badges222 silver badges250 bronze badges asked Aug 10, 2011 at 10:11 antonjsantonjs 14.3k15 gold badges70 silver badges91 bronze badges 1
  • 1 Both return false in normal use (i.e. not within the Firebug or browser console, which use eval()). See my answer. – Tim Down Commented Aug 10, 2011 at 13:14
Add a ment  | 

2 Answers 2

Reset to default 6

Actually, neither should return true, and indeed they don't in Firefox or Chrome (untested in other browsers). I imagine you tested this with Firebug or another browser console, which changes things due to the console using eval(). delete only deletes properties of an object and cannot normally delete a variable declared using var, whatever the scope.

Here's an excellent article by Kangax on the subject: http://perfectionkills./understanding-delete/

Edit: Both return false in normal use (i.e. not within the Firebug or browser console, which use eval()). See Tim Down’s answer (it should be the accepted one).

发布评论

评论列表(0)

  1. 暂无评论