Javascript 从JS内存中清除对象

Javascript 从JS内存中清除对象,javascript,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);

因此,我试图找出一种方法,在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
} );

fruit=null将使
水果无效


如果要删除变量
水果
,并且该变量是在全局上下文中定义的,则可以使用
删除水果

不完全确定它是否会触发垃圾收集,尽管您尝试过
删除myObject
fruit=null?如果对象没有被任何变量引用,它将在某个时刻被自动垃圾收集。