开发者

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开发者_StackOverflow社区 I get rid of the global variable?


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)

remove global variable


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.


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); 


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜