1

Please see this example:

var a = new A();
b = a;

A is an object I want to delete b and at the same time all pointers which point to same object! But I have access only too b. please give me solution that I can remove b and a at the same time by accessing only to b!

Aryan
  • 2,675
  • 5
  • 24
  • 33
  • 1
    As far as I know, you cannot [delete](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete) variables. You can only delete properties (global variables are properties of the *window* object). – ComFreek Aug 21 '13 at 11:46

2 Answers2

3

If you don't assign temporary values to global variables, you should not even care about the problem you describe, since it's taken care for by a garbage collector. The principle is very simple: as soon as a value loses all pointers to it, it gets wiped out from the memory by a garbage collector.

For instance, in the following example the variables a and b exist only for as long as the function f executes:

var f = function () {
  var a = new A();
  var b = a;
}

Therefor since by the end of execution of f the value new A() loses all pointers to it, it gets wiped out by the GC.

Nikita Volkov
  • 42,792
  • 11
  • 94
  • 169
1

a solution that "removes" b and a at the same time by accessing only to b!

You cannot remove all existing references by one command, you will need to do it manually (ask every reference holder to forget its value).

You will need to access all variables holding the value, in here

b = null;
a = null;
// now it can be garbage-collected
Bergi
  • 630,263
  • 148
  • 957
  • 1,375