what's the javascript function scope in firefox components?
I am making a firefox component using javascript.
But i am always confused about what 开发者_运维技巧is the global scope of the current javascript function, which results in the following questions?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?
Is there a way to show(print some information) the current global scope of a javascript function?
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 viaComponents.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 byComponents.utils.evalInSandbox( <code> , sandbox);
, and<code>
contains afunction sandboxFoo()
that callexternal.foo()
1). what's the global scope of
sandboxFoo
when it is running? I think it should be2). what's the global scope of
external.foo
when it is called bysandboxFoo
? Is it the<scope1>
or<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
this
points to when you assign it. Assuming thatthis
is the global scope of your component (it likely won't be as written), and that you mean that you callexternal.foo
from 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.
精彩评论