- I understand some basic concept about global scope of js function in normal case, but i want to know, when is the global scope of a function determined? The time when function is created(defined), or the time when the function is called?
following question is firefox component specific
For firefox component, does each component have a global scope itself? (which means each function of the component will be run in itself global scope ), or every components have the same global scope?
If same, what's that?
For example, in such case
sorry for this boring example, i just make it as clear as possible.
I make a sandbox via
Components.utils.Sandbox(<scope1>). I define some function in a ff component( i called
<scope2>) ,and inject a variable in to sandbox by :
sandbox.external = this;( "this" is just a component itself, which in a scope2 )
After these step, i run some code in sandbox by
Components.utils.evalInSandbox( <code> , sandbox);, and
function sandboxFoo()that call
1). what's the global scope of
sandboxFoowhen it is running? I think it should be
2). what's the global scope of
external.foowhen it is called by
sandboxFoo? Is it the
<scope2>? Any documentation?
- The global scope of a function is figured out when you define a function. In the case of components, the global scope is shared with everything in the file (most of the time, you'll only have one component per file, so that component effectively gets its own global scope).
- This really depends on what you want to know, and how your function was called.
- The global scope of an XPCOM component is going to be the file it is defined in. If you have more than one component defined in the file (uncommon), they will share the same global scope.
- I think you mean what is the global scope when running code inside the sandbox (your question is vague, but I can revise this if I'm wrong). When you create a sandbox, it creates a new global scope for the sandbox.
- This is a bit more complicated. It's a reference to whatever
thispoints to when you assign it. Assuming that
thisis the global scope of your component (it likely won't be as written), and that you mean that you call
external.foofrom within the sandbox, the global scope will be your components global scope.
- The scope is established when the function is defined.
- Sorry, don't know of any.
- The scope depends on what you are overlaying. If two components are overlaying the same element, they have the same scope.
- The global scope will be the one in which it was defined.