I have a function that assigns an object to a custom namespace, without overwritting existing objects in the path, a bit like mkdir -p, for example:
assignObjectToNamespace(myObj, "com.stackoverflow.questions.root", window)
The prototype would is something like:
assignObjectToNamespace(object, namespace, target)
So you create the namespace on target and assign the object to it. Obviously if com.stackoverflow already exists on target you don't want to overwrite it/initialize it.
This method will be used mostly in a browser context, so instead of passing window as the target I would like the target to be optional, and default to the root element.
Somewhere in the implementation I have:
if(!target) return
var recursiveElement = target
and then recursiveElement is going to be assigned to the second object in the namespace, the third and so on and so on, until there's no more . separated elements in the namespace.
If I want to change the code to
var recursive element = target || root
What should root be?
The reason why we don't default root to window is to make this library a bit more general and avoid tying it to the browser context. I know it's not a biggie, but instead of just changing the code to adapt it or just giving up and making the third argument mandatory, I wanted to know if there's a quirk out there that I don't know of.