I'm reading book about OO programming in JavaScript and get some strange behaviour:
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);
Could anyone understand, why my delete VARIABLE return false (in Firefox) and strict mode display warning like:
SyntaxError: applying the 'delete' operator to an unqualified name is deprecated
console.log('delete foo: ', delete foo);
You can't delete a normal variable in javascript. You can delete a property on an object, but not a variable. Thus, what you're trying to do isn't allowed. If you want to free the contents of that variable (assuming there are no other references to the data it points to), you can just set the variable to null
.
The part of the message about unqualified
probably just refers to the fact that specifying a property would have to be more than just an unqualified name like you have. It would have to have an object reference.
As DCoder mentions in a comment, this is a good reference for understanding the delete operator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With