0

Recently I read a blog post (which I unfortunately lost track of) that showed code like this:

// in global scope
var app = this;

greet = function() { return 'Hello World!' };
var sayGoodbye = function() { return 'Bye' };

delete greet;
delete sayGoodbye;

sayGoodbye(); // 'Bye'
app.greet(); // undefined

And mentioned this as a solution for being able to delete a variable (which is not possible) by turning global variables into object properties of the global scope. However are there any dangers/ gotcha's to doing this? What is the true benefit of it?

webketje
  • 10,376
  • 3
  • 25
  • 54
  • Why would you need to delete "variables"? Doesn't make any sense at all when you can work with object properties. – dfsq Jun 14 '14 at 15:15
  • 1
    Read http://stackoverflow.com/questions/1596782/how-to-unset-a-javascript-variable – CnapoB Jun 14 '14 at 15:16
  • If you find yourself in a situation where you need to do manual memory management in JS, there's a very good chance that you don't actually need to do manual memory management. – JJJ Jun 14 '14 at 15:18
  • As I stated, this code comes from a blog post, and I want to know why someone would want to do this, ie. whether there are any benefits or dangers. I am aware that JS memory management in all but the rarest cases needn't be a concern for the coder. – webketje Jun 14 '14 at 15:36
  • 1
    This was probably only an example to demonstrate [how delete works](http://perfectionkills.com/understanding-delete/). You are never going to do this in actual code. [Don't use global variables](http://stackoverflow.com/q/10525582/1048572) – Bergi Jun 14 '14 at 15:53
  • 1
    There are a couple of situations where a global variable (or rather an object) can take place: When you have to interact with an other window, or when you've multiple separately loaded scripts interacting with each other. Everything else can, and should be done without globals. – Teemu Jun 14 '14 at 16:20
  • @Teemu Thanks, that provides an answer to my question. – webketje Jun 14 '14 at 16:33

1 Answers1

0

I work always in local scopes where the variables(objects) gets disposed by the garbage collector when they are no longer in scope (needed). and I don't recommend working inside the global scope, only for functions you need throughout your whole program and should always be available and so never has to be deleted.

otherwise you shouldn't use the global scope. Its one of the common rules for programming, that you should avoid using globals. Globals are not safe, everything in your program can access it and modify.

You should not use it, if you can avoid it with locals(inside functions).

Instead of using a lot of globals, pass local variables as parameters(arguments) trough functions. That way unauthorized functions cannot access variables( read or write to it).

(memory) Also global variables will not be collected by the garbage collector because there will always be a reference to it. So you would have memory allocated that you wouldn't use anymore. or in the function and not relevant for now.

Also relevant to your question: delete only works on object properties.

If you mean by global the full global of the web application. app = this catches the global(window) so it is not really necessary to get this instance because it is already global.

Zezura
  • 128
  • 1
  • 9