I was reading this article on the Javascript delete method, and tried this code:
function f(){
var x = "abcd";
console.log(delete x); //returns false, because x has DontDelete attribute
y = "abcd";
console.log(delete y); //returns true, but I didn't explicitly assign y as a property
}
This is strange, because I am not using property assignment for y (as in: window.y="abcd";), which the article claims is how you make properties deleteable.
Why does y not have the DontDelete attribute?
UPDATE:
In the answers below, it is claimed that y is deleteable merely because it is a property of the window object. That is not true. Consider the following snippet:
function f() {
var functionx = "abcd";
console.log(delete functionx); //returns false, because x has DontDelete attribute
functiony = "abcd";
console.log(delete functiony); //returns true, but I didn't explicitly assign functiony as a property to the window
}
//take 1
var globalx = "abcd";
console.log(this.globalx);//to prove x is a property of the global
console.log(delete globalx);//fails, even though x is a property of the window
//take 2
this.globaly = "abcd";
console.log(this.globaly);//to prove y is a property of the global
console.log(delete globaly);//succeeds, because I used property assignment
f();
In take 1, globalx is a property of the window, but it is still undeleteable. The criteria for whether delete will succeed is not whether the variable is a property of something (which is always true), but whether that property has a DontDelete attribute set.
delete x is invalid because it does not resolve to any object property.
y resolves to a property of the window object.
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/delete:
delete expression
where expression should evaluate to a property reference
Here is the expanded version of the second example:
window.y = "abcd";
console.log(delete window.y);
It's pretty obvious now what's happening.
If you don't use the var keyword, the interpreter assumes the variable is a property of the global object, (which is usually window), as shown in this JSFiddle. Note that in the global scope, this is the same as the window object.
Re: Update:
Outside a function, a variable declared with var is defined as a non-configurable property of the global object. This is detailed in https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/var, where it says:
Using
varoutside a function is optional; assigning a value to an undeclared variable implicitly declares it as a global variable (also a property of the global object). The difference is that a declared variable is a non-configurable property of the global object while an undeclared is configurable.
Declaring a variable inside a function does not assign it to a global property, but rather a local variable.
That explains the behaviour you're seeing when defining a variable in the global scope.
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