从JS内存中清除对象

时间:2014-05-26 08:15:26

标签: javascript

所以我试图想出一种方法来触发在javascript中创建的对象上的垃圾收集。我有一个对象被传递函数作为回调的参数,并想知道是否有一个合适的方法"无效"我在其中一个回调函数中创建的对象。

见下面的例子。

var fruit = new gameObject( "fruit", 20, 20, 30, 30, function(x,y,width,height,meta){

  //Render the gfx.
  gfxFruitRender(x,y,width,height,meta);

}, "s", function(x,y,meta,object){

  //On click.
  gameSfxTap.play();
  clearInterval( object.Animationinterval );
  objectDelete( object.objectId );

  //How can I reference the var fruit here, sort of like this = null; (I know this wont work though)


}, {
  "type" : "lime",
  "points" : 1,
  "rotation" : 0
} );

1 个答案:

答案 0 :(得分:1)

fruit = null;将取消fruit

如果要删除变量fruit,并且已在全局上下文中定义,则可以使用delete fruit;

相关问题