If the code in the area is guaranteed to be synchronous, you can create a function that subsequently calls the destructor. This may not be as flexible, and the syntax may not be as neat as in C ++, but:
var M = function() { console.log("created"); this.c = 0; }; M.prototype.inc = function() { console.log("inc"); this.c++; }; M.prototype.destruct = function() { console.log("destructed", this.c); }; var enterScope = function(item, func) { func(item); item.destruct(); };
You can use it as follows:
enterScope(new M, function(m) { m.inc(); m.inc(); });
This will be recorded:
created inc inc destructed 2
pimvdb Nov 04 2018-12-11T00: 00Z
source share