9

I've been looking through a lot of blog posts, documentation, etc. about JavaScript's strict mode.

I noticed that there are a lot of restrictions on the delete keyword. I don't even know if you could call them restrictions. It seems like delete just no longer works.

I would love to use strict mode. It's a great idea. But I also think delete is a great idea.

Are there any alternative ways to "delete" a variable?

McKayla
  • 6,467
  • 4
  • 29
  • 45
  • Mind telling us what you are trying to achieve? As it stands the question reads like a rant. – Oded Apr 14 '11 at 19:15
  • 2
    Sounds like hes looking for an alternative to the delete keyword to delete a variable in javascript... – clamchoda Apr 14 '11 at 19:23

3 Answers3

4

You do not delete variables.

delete is used to remove a property from an object.

delete foo.a will remove property "a" from object foo.

Why do you need to remove a local variable from scope? You can just set the variable to be undefined

(function(undefined) {
    // undefined is undefined.
})();

(function() {
    var undefined; // undefined is undefined
})();

Another way to check againts undefined would be doing foo === void 0 since void is an operator which runs the expression following it and returns undefined. It's a clever trick.

Raynos
  • 156,883
  • 55
  • 337
  • 385
  • It's not recommended to set variables to the value of `undefined` since in non-strict JS versions the value of `undefined` can be manipulated. – Eli Apr 14 '11 at 19:34
  • 1
    @Eli but your going to have a local value of `undefined` right? Besides if you find some code that manipulates undefined burn it then ban the author from using javacript. – Raynos Apr 14 '11 at 19:38
  • It isn't local. It's a global variable called `config`. – McKayla Apr 14 '11 at 19:40
  • @tylermwashburn `delete window.config`. But seriously why does it need to be global, there is a better pattern to what your doing, your just not aware of it. – Raynos Apr 14 '11 at 19:41
  • 1
    I can't think of an instance where I've ever needed it. For comparison's it is always recommended to type compare and not value compare: `typeof foo === 'undefined'` is preferred over `foo == undefined`. A variable should be garbage collected once the engine determines that it will no longer be accessed by determining if it can be referenced externally. This is why proper scoping is so important. It does not matter if it's undefined, because setting a variable to undefined will not destroy it. I highly recommend this read: http://perfectionkills.com/understanding-delete/ – Eli Apr 14 '11 at 19:41
  • And if someone changes the value of `undefined` in the local scope? I know the chances of this are small, but it's about the best practice. If you shouldn't use it in one way, why is it ever a good idea? – Eli Apr 14 '11 at 19:44
  • @Eli it's only neccesary to check with `typeof` if the variable might not exist. If the variable exist and I want to know if it has a defined value I check for `variable === undefined`. – Raynos Apr 14 '11 at 19:45
  • @Eli If someone changes the value of `undefined` in local scope either a) you have an incompetent co-worker or b) your using `eval`. For a) you teach him how to write javascript and fix the bug. for b) you stop using `eval`. – Raynos Apr 14 '11 at 19:46
  • It's your own decision to compare against undefined, I'm just letting you know that most JS gurus recommend against this, because undefined is mutable. What if before your `variable === undefined` check I put `undefined = 3;`? Your code would fail. I understand the chance of this is small, but someone could easily break something by saying `if (variable = undefined)`. It's a lot more subtle, but the concept still applies. – Eli Apr 14 '11 at 19:48
  • I understand someone shouldn't do it, but sometimes you can't stop stupid. This is just the reason it's not done. – Eli Apr 14 '11 at 19:49
  • @Raynos What would that way be then? – McKayla Apr 14 '11 at 19:49
  • http://stackoverflow.com/questions/4725603/variable-undefined-vs-typeof-variable-undefined – Eli Apr 14 '11 at 19:50
  • @Eli I don't need to write idiot proof code. Writing hyperly defensive code is just as bad as micro optimisation. it's just not neccesary. If someone puts `undefined = 3` into my code it becomes their problem not mine. What your talking about are bugs and typos that will be fixed. Code like that _does not_ go into production. Code like that _does not_ pass peer review. – Raynos Apr 14 '11 at 19:50
  • All I am trying to say is that while you CAN compare to undefined, it is considered a bad practice to do so. – Eli Apr 14 '11 at 19:51
  • 1
    @Eli it's bad practice to compare againts the _global_ `undefined`. comparing againts a local `undefined` is safe. – Raynos Apr 14 '11 at 19:51
  • @Raynos What if you botch up some code and forget some equal signs? `if (variable = undefined)`. It's a common mistake, why take the chance it could ever happen. – Eli Apr 14 '11 at 19:52
  • Sorry @Raynos, they can all be modified. That's why strict JS in the future undefined is immutable because of this problem. – Eli Apr 14 '11 at 19:53
  • @Eli code review. Testing. etc. What you have there is a _bug_. bugs do not go into released code. It's the same as a typo like doing `winddw.createElement` breaks. I don't defend againts that because bugs like that are picked up by review & testing, not to mention syntax checkers and jslint. – Raynos Apr 14 '11 at 19:53
  • 1
    I understand that it is wrong. But you can't always prevent bugs from going into production. I am not pulling these best practices out of thin air. This has been recommended time and time again by brilliant JS engineers and for good reason. I am just trying to inform you of how others avoid ever needing to run into the problem to begin with. – Eli Apr 14 '11 at 19:55
  • @Eli there is nothing wrong with the `typeof` check, its perfectly valid. but there's no reason to ban the use of `undefined` as a local variable in source you have control over. "can't always prevent bugs from going into production" that is correct. But doing `=` in an if statement or assigning a value to `undefined` is not one of those bugs. The bugs that go into production are either race conditions, complex edge cases or bugs by design. It should simply be personal choice to use `undefined` locally or not. – Raynos Apr 14 '11 at 20:02
  • I am not trying to say that you should change the way you are doing things. I just hate to see a practice promoted that goes against an **established best practice**. – Eli Apr 14 '11 at 20:03
  • I totally agree: if you wish to compare against undefined that is up to you. I just am trying to make others aware that it is recommended to be done another way. – Eli Apr 14 '11 at 20:05
  • @Eli again **established best practice** is to not use global `undefined`. A local copy of `undefined` should be considered safe if you have control over your source code and are competent. Apart from Crockford name me a respectable expert who bans the usage of `undefined`. – Raynos Apr 14 '11 at 20:06
  • But it is inconsistent. Instead of doing it two different ways depending on your environment, you can do it the same way in all. It's a no brainer for maintenance. – Eli Apr 14 '11 at 20:07
  • @Eli "depending on your environment" what do you mean? How is it inconsistent? If I'm in an environment where `undefined` is abused I will _refactor_ that for them. – Raynos Apr 14 '11 at 20:09
  • John Resig, Stoyan Stefanov, Nicolas Zakas to name a few. – Eli Apr 14 '11 at 20:10
2

How about just setting your variables to null? Once you set the variables to null, the JavaScript garbage collector will delete any unreferenced variables on next run.

HTH.

EDIT: As @chris Buckler mentioned in the comments, this can't be done at global scope, as global variables never get garbage collected.

Karl Nicoll
  • 14,491
  • 3
  • 46
  • 60
  • 1
    Doing this will assing the value to null but the variable will not actually be destroyed. – clamchoda Apr 14 '11 at 19:31
  • @Chris - Setting it to null will *normally* (if there are no more references to the variable) make it eligible for GC. Unfortunately this doesn't apply to global variables according to http://stackoverflow.com/questions/864516/what-is-javascript-garbage-collection, but the question didn't enlighten us to the fact that the OP was referring to a global variable. – Karl Nicoll Apr 14 '11 at 19:36
  • If you were to do something like `for (prop in global)`, `config` would still be run for. – McKayla Apr 14 '11 at 19:39
2

As others are alluding to, you should never really need to delete variables. It sounds more like an issue of not properly controlling scope. If you keep your variables in a function scope, they will be deallocated from memory once they are no longer referenced.

Do you have another global namespace other than the global window namespace? It would probably benefit you to have something like that for this situation:

(function(global) {

    var Application = {};

    Application.config = { /* config stuff */ };

    global.Application = Application;

})(window);

// if you need to remove config, you can remove it from
// your object and not the window object:
delete Application.config;

For a real in-depth understanding of deleting and undefined in JS:

http://perfectionkills.com/understanding-delete/

http://scottdowne.wordpress.com/2010/11/28/javascript-typeof-undefined-vs-undefined/

How do I check if an object has a property in JavaScript?

Community
  • 1
  • 1
Eli
  • 15,671
  • 4
  • 34
  • 48
  • I have a global `config` variable that's used to configure some stuff in a JavaScript file. I want to delete it after it's done loading, but it just throws an error, and leaves it there. The line is `delete global.config;`. – McKayla Apr 14 '11 at 19:27
  • The config object has to be defined before the script is run. – McKayla Apr 14 '11 at 19:36
  • That won't affect the applicability of what I've said. Do you have a sample of how you're config is initialized? – Eli Apr 14 '11 at 19:37
  • @tylerwashburn if you want to delete from global scope delete from the `window` object. Global scope and the window object are the same. Just `delete window.config` – Raynos Apr 14 '11 at 19:40