User0123456789
User0123456789

Reputation: 303

Is this a circular reference inside the closure?

I was wondering if self cause a circular reference by being captured by add. And will this be a problem for a garbage collector of an old browser.

var fun = function() {
    var self = this;
    this.value = 0;
    this.add = function(number) {
        self.value += number;
    };
};
fun.prototype.inc = function() {
    this.value++;
};
fun.prototype.dec = function() {
    this.value--;
};

Upvotes: 0

Views: 32

Answers (1)

Bergi
Bergi

Reputation: 664920

I was wondering if self cause a circular reference by being captured by add.

Yes.

And will this be a problem for a garbage collector of an old browser.

No. Not even an ancient one.

Upvotes: 1

Related Questions