JavaScript weak link JavaScript chrome code

Suppose I write a class A in my Jetpack-based library (i.e. we are talking about chrome / extension code) :

function A() {
  //constructor
}
A.prototype = {
  //class members
};

The user creates an instance of the class A:

var a = new A();

Now suppose I want to track all the instances Athat were created, I could do:

var listA = [];

and add listA.push(this);to the constructor A, as a result we get:

var listA = [];

function A() {
  // constructor
  listA.push(this);
}

A.prototype = {
  // class members
}; 

, A (, A ). : listA A, . , listA A, . .

listA . , -, , . - :

var weak_a = new weakRef(a);
assert(weak_a.ref === a);

, , , . listA. ?

+5
1

, . , , Components.utils.getWeakReference . , , JavaScript.

+3

All Articles