Ido Tamir
Ido Tamir

Reputation: 3127

remove global variable

I use limejs and I declare a function like:

function up(){
   var pos = global.getPosition();
   pos.x += 2;
   global.setPosition(pos);
}

and call it using

lime.scheduleManager.schedule(up,global);

How do I get rid of the global variable?

Upvotes: 3

Views: 12258

Answers (5)

hvgotcodes
hvgotcodes

Reputation: 120178

you can always delete a property on an object like so (note that global variables are really on window, at least in browser javascript)

delete window.global;

if you then try to access your variable, you will get a Reference error. Be careful, because if your schedule call invokes the method later, and it depends on global, it will be gone. Might be better just to use a local variable (with var)

enter image description here

Upvotes: 7

Ruan Mendes
Ruan Mendes

Reputation: 92274

If you mean mark it so it can be garbage collected, you can just set the reference to null. If there are no other references, the object will be gc'd next time the gc kicks in.

Upvotes: 0

usoban
usoban

Reputation: 5478

Since second parameter to schedule is context object and you pass global to it, you could write the function as

function up(){
   var pos = this.getPosition();
   pos += 2;
   this.setPosition(pos);
}

If that is really what you're asking for.

If you only need to delete the variable, you can set it to null. Operator delete is intended for deleting object properties, not unsetting variables.

Upvotes: 1

jtfairbank
jtfairbank

Reputation: 2307

In regular javascript it would be:

delete global.myVar

or

delete pos

Not 100% sure if this will work in limejs (never used it) but worth a try.

Upvotes: -1

josh3736
josh3736

Reputation: 144882

If you're not referencing that function anywhere else, why not just:

lime.scheduleManager.schedule(function (){ 
   var pos = global.getPosition(); 
   pos.x += 2; 
   global.setPosition(pos); 
}, global); 

Upvotes: 1

Related Questions