It is often helpful to have objects with a lifetime tightly related to scope. For example in C++, the destructor for auto objects. In C#, Dispose
within a using
block.
I want to know if there's a way to do this in Javascript.
The example that comes to mind is a logging object that can measure the time spent in a function. Something like this:
In C++:
void someFunction()
{
LogScopeMessage lsm("in some function");
...
}
In C#:
private void someFunction()
{
using(auto lsm = new LogScopeMessage("in some function"))
{
...
}
}
.... and in Javascript what would be the best way to set up this kind of idiom?