I tried to create a method which can delete itself while instantiating.
After several failed attempt I ended up writing this evil rem()
var g = function () {
this.rem = function () {
var _instance = this;
setTimeout(function () {
console.log('_instance before:', _instance, 'scope:', this);
delete _instance;
console.log('_instance after:', _instance);
}, 10);
return this;
};
return this;
}
I know it looks ugly. But it's a bit strange for me that this not working either. Inside the anonymous function the scope is window
and the _instance
variable is seems to be referring to the desired instance as well.
var t = new g();
t.rem();
outputs:
_instance before: g {asdf: 3, rem: function}, scope: Window {top: Window, window: Window, location: Location, external: Object, chrome: Object…}
_instance after: g {asdf: 3, rem: function}
What is the reason it is not working?
Thanks.