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

javascript - Why is my deleted function not typeof "undefined" in Node.js? - Stack Overflow

programmeradmin0浏览0评论

I'm using Node.js.

After my "sum" function gets deleted, I would expect the typeof(sum) to return "undefined", but it doesn't.

// functions are data in Javascript

var sum = function ( a,b ) { return a + b; }
var add = sum;
delete sum;
console.log ( typeof sum ); // should return undefined
console.log ( typeof add ); // should return function
console.log ( add( 1,2 ) ); // should return 3

I would think it should return:

undefined
function
3

But instead it returns:

function
function
3

I'm using Node.js.

After my "sum" function gets deleted, I would expect the typeof(sum) to return "undefined", but it doesn't.

// functions are data in Javascript

var sum = function ( a,b ) { return a + b; }
var add = sum;
delete sum;
console.log ( typeof sum ); // should return undefined
console.log ( typeof add ); // should return function
console.log ( add( 1,2 ) ); // should return 3

I would think it should return:

undefined
function
3

But instead it returns:

function
function
3
Share Improve this question edited Oct 2, 2011 at 2:37 EhevuTov asked Oct 2, 2011 at 2:30 EhevuTovEhevuTov 20.5k16 gold badges68 silver badges71 bronze badges 14
  • 2 What's it returning? function, I assume, but just checking. – Doozer Blake Commented Oct 2, 2011 at 2:33
  • It works for me (in Firebug). Also, you're missing a ;. – SLaks Commented Oct 2, 2011 at 2:33
  • 1 @SLaks - When you say "It works for me", what does that mean exactly? – Jared Farrish Commented Oct 2, 2011 at 2:35
  • 1 @SLaks - Well... this actually give me Function Function 3 in Firefox 6.0.1/Firebug. – Jared Farrish Commented Oct 2, 2011 at 2:38
  • 3 @JaredFarrish, The Firebug's console uses eval behind the scenes and the variable environment bindings on eval code are mutable, that's why delete actually works there... Try: (function () { eval('var a'); return delete a; })(); and you will see that delete a successfully returns true... I might expand this on my answer... – Christian C. Salvadó Commented Oct 2, 2011 at 2:49
 |  Show 9 more comments

2 Answers 2

Reset to default 12

You shouldn't use the delete operator on identifiers (in scope variables, functions - as sum - or function arguments).

The purpose of the delete operator is to delete object properties.

When you declare a variable a function declaration or function arguments, behind the scenes these identifiers are actually a properties that belongs to the environment record of the current scope where they were declared.

Those properties are explicitly defined internally as non-configurable, they can't be deleted. Moreover, the usage of the delete operator has been so misunderstanded that on ES5 Strict Mode, its usage on identifiers has been completely disallowed, delete sum; should throw a ReferenceError.

Edit:

As @SLacks noted in the question comments, the delete operator does work with identifiers from the Firebug's console, that's because the Firebug uses eval to execute the code you input in the console, and the variable environment bindings of identifiers instantiated in code executed by eval, are mutable, which means that they can be deleted, this was probably to allow the programmer to delete at runtime dynamically declared variables with eval, for example:

eval('var sum = function () {}');
typeof sum; // "function"
delete sum; // true
typeof sum; // "undefined"

You can see how this happens also on the console:

And that's probably what happened with the book you are reading, the author made his tests on a console based on eval.

delete is only for deleting properties of object notations and not for deleting the declared variables as per this article.

The snippet you have posted is there almost exactly as you've it here.

EDIT: The same article referenced above clarifies the inconsistency that appears in Firebug as well in this section. Relevant excerpt:

All the text in console seems to be parsed and executed as Eval code, not as a Global or Function one. Obviously, any declared variables end up as properties without DontDelete, and so can be easily deleted. Be aware of these differences between regular Global code and Firebug console.

发布评论

评论列表(0)

  1. 暂无评论