Firebug gives the use of the delete operator to an unqualified name, deprecated

I read a book on OO programming in JavaScript and get weird behavior:

function f1() { var a = 1; console.log('a in f1 function ', a); console.log('f2() called ', f2()); return 'f1 return value'; } function f2() { console.log('a value in f2() ', a); return a; } var a = 5; a = 55; var foo = 'bar'; console.log('delete a: ', delete a); console.log(a); console.log(f1()); console.log('delete window.f2: ', delete window.f2); console.log(f1()); console.log('delete foo: ', delete foo); 

Can anyone understand why my delete VARIABLE returns false (in Firefox) and a strict mode warning, for example:

 SyntaxError: applying the 'delete' operator to an unqualified name is deprecated console.log('delete foo: ', delete foo); 
+4
source share
1 answer

You cannot delete a regular variable in javascript. You can remove an object property, but not a variable. Thus, what you are trying to do is not allowed. If you want to free the contents of this variable (if there are no other references to the data that it points to), you can simply set the variable to null .

The unqualified part of the message probably just refers to the fact that the property declaration should be more than just an unqualified name like yours. It must have a reference to the object.

As DCoder mentions in a comment, this is a good reference for understanding the delete operator.

+9
source

All Articles