0

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.

bitoiu
  • 6,893
  • 5
  • 38
  • 60
  • thank you @Bergi and apologies for the duplication. – bitoiu Jul 03 '14 at 10:47
  • Oh, it's fine, you seem to have not know the technical term "global object". This question will guide other askers that don't either. – Bergi Jul 03 '14 at 10:53
  • 1
    Which led me just a few minutes ago to look at: http://stackoverflow.com/questions/21578446/what-is-the-root-object-in-node-js – bitoiu Jul 03 '14 at 11:01

1 Answers1

2

When not in the browser context usually the root context is called global, so you could use that.

So you could do:

var context = typeof window === 'undefined' ? global:window;

Or more generically - without referencing global or window:

var customGlobalContext;
(function() { customGlobalContext = this; }).call(null);

The latter works because any method executed without a context ( simply f() or f.call(null) ) will be executed in the global context, and this will refer to it.

Update

While the first approach is still good, the second one might run into problems in strict mode.

After @Bergi's comment, I've read this interesting article, which explains the in and outs of the this keyword.

After it, I'd say, in order to get the global context, have this line in the root of the file (so not wrapped in any method/function):

var customGlobalContext = this;

In this case this will refer to the global context wherever you are (nodejs/window/etc.)

Community
  • 1
  • 1
Matyas
  • 13,473
  • 3
  • 60
  • 73
  • 1
    I like the first one @Matyas, I'll wait a couple of hours and if this is the best solution, I'll accept it. – bitoiu Jul 03 '14 at 09:31
  • 1
    `.call(null)` will only work in sloppy mode, which should be avoided. – Bergi Jul 03 '14 at 10:41