简体   繁体   中英

why can't global variables be deleted?

How come a properly declared global variable can't be deleted?

I don't know if this is across all program languages, but I know that in JavaScript it can't be deleted.

Source: Javascript the Definitive Guide O'Reilly.

When you use global variables and you want to be able to delete them, you should easily define them in a global object, without using var in your statement, like:

let's say you want to define a global varible in your code, and you need to be able to delete them whenever you want, so if you do:

function myfunc(){
    var name = "Robert";
    console.log(delete name);
}

and call it in your console you would have, false as the result of delete statement, which means it has not got deleted, but if you do it like:

function myfunc(){
    var obj = {};
    obj.name = "Robert";
    console.log(delete obj.name);
}

then your result would be true, which means it gets deleted now.

now for global object if you create it like:

window.myobj = {};

then you can delete it and it actually get deleted:

delete window.myobj;

or

delete window["myobj"];

The thing is when you create your variable using var , in the window context, although it is on object in the window, but it doesn't get deleted, for instance if you do:

var myobj = {};

in the browser dev console, it gets defined in the window, and you can have it like:

window.myobj

but you can not delete it, because you have defined it in a var statement.

But do not forget to set it to null , if you really want it to get deleted from memory:

window["myobj"] = null;
delete window["myobj"];

As was stated in this answer by user Eric Leschinski

Delete a variable in JavaScript:

Summary:

The reason you are having trouble deleting your variable in JavaScript is because JavaScript won't let you. You can't delete anything created by the var command unless we pull a rabbit out our bag of tricks.

The delete command is only for object's properties which were not created with var.

JavaScript will let you delete a variable created with var under the following conditions:

  1. You are using a javascript interpreter or commandline.

  2. You are using eval and you create and delete your var inside there.

or you can set null to an variable which will behave like a deleted object

When variable is created in global scope then automatically DontDelete property is added to the variable and set to the true. That is the reason global variables (or functions too) can not be deleted.

For other variables that property is false so those can be deleted.

For more clarity you can refer the article : understanding delete

With ECMAscript 5, the properties added to an object now have attributes which allow you more control over the object. These attributes are:

  1. value - The actual value of the property
  2. writable - If the property can/cannot be changed.
  3. configurable - If set to false,any attempts to change its attributes will fail in strict mode (and will return false in non-strict mode)
  4. enumerable - if the property can be iterated over when the user does for (var prop in obj) {}

These attributes can be checked with another API exposed by Ecmascript 5 called:

Object.getOwnPropertyDescriptor(obj, prop)

Now, when you create a global variable WITHOUT the 'var' keyword, like so:

sum = function (a, b) { return a + b; }

then this property 'sum' get created on the window object with configurable attribute set to true .

console.log(Object.getOwnPropertyDescriptor(window, "sum"))

... and therefore this property CAN be deleted from the window object.

delete window.sum //returns true

But when you create a property with the var keyword , then configurable property is set to false like so:

var multiply = function (a, b) { return a * b; }
console.log(Object.getOwnPropertyDescriptor(window, "multiply"))

... and now, this property CANNOT be deleted .

delete window.multiply //returns false in non-strict mode

Courtesy: John Resig

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM