JavaScript closure variable sharing
JavaScript closure variable sharing between instances
JavaScript
// https://stackoverflow.com/questions/54905462
(function (w) {
var self;
w.Test = function (name) {
// self = undefined;
// delete self;
self = this;
self.name = name;
};
w.Test.prototype.getName = function () {
return self.name;
}
w.Test.prototype.test = function () {
console.debug(self.name);
console.debug(self == this);
}
})(window);
var a = new Test("a");
var b = new Test("b");
console.log(a.getName() + b.getName());
// expected: ab
// actual: bb
a.test();
b.test();
// expected: a > true > b > true
// actual: b > false > b > true