Although there are numerous techniques available on Stack Overflow to ensure a function is only called once, none of them seem to fit my specific scenario and objectives.
In this case, the function is nested within an object literal, and I aim to either add this utility to Function.prototype
or make it more concise in some way.
Currently, I am following this approach:
var HugeLiteral={
subComponent:{
_name:'subComponent'
,_privateFnsEtc:function(){
}
,_init:function(){
// start of thing I would like to "Macro-ize"
if (arguments.callee.initialized) {console.error('already initialized');return;}
arguments.callee.initialized=true;
// End
//
// I wish to do something like
if (!arguments.callee._proceed()) {return;}// _proceed logs the error
//
// or alternatively
globalRunOnceCheckFn();// uses stack to get this function and do a throw
// or
maybeWrapCallingFunction(this);
//
codeToExecuteOnceHere();
//
// or this method which would tend to lock me into a single return point
this._init=this._init.guardFnOnFunctionPrototype;
}
}
,_init:function(){
// Here I could walk through HugeLiteral and perform tasks as needed,
// but I am unsure about wrapping the _inits.
}
};
My primary requirement is that the implementation must work internally within the function. While wrapping the function externally could achieve the goal of running it only once, being able to handle it from inside the function itself is crucial for me.