3

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?

  1. 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?

  2. Is there a way to show(print some information) the current global scope of a javascript function?

    following question is firefox component specific

  3. 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?

  4. 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 <code> contains a function sandboxFoo() that call external.foo()

    1). what's the global scope of sandboxFoo when it is running? I think it should be

    2). what's the global scope of external.foo when it is called by sandboxFoo? Is it the <scope1> or <scope2>? Any documentation?

Wayne
  • 59,728
  • 15
  • 131
  • 126
winterTTr
  • 1,739
  • 1
  • 10
  • 30

2 Answers2

1
  1. 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).
  2. This really depends on what you want to know, and how your function was called.
  3. 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.
    1. 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.
    2. This is a bit more complicated. It's a reference to whatever this points to when you assign it. Assuming that this is the global scope of your component (it likely won't be as written), and that you mean that you call external.foo from within the sandbox, the global scope will be your components global scope.
sdwilsh
  • 4,674
  • 1
  • 22
  • 20
  • Is it possible to refer to the global object in a component (the way you do with `window` in the browser)? Does that host object have any properties? – Wayne Mar 04 '11 at 16:35
  • You could add your own global variable for it, but no automagic define. – sdwilsh Mar 04 '11 at 20:16
  • Unless the code is invoked as an object method, "this" will refer to the global scope by default. – Neil Mar 06 '11 at 00:24
  • However, it's a component so it very likely is being invoked as an object method. – sdwilsh Mar 06 '11 at 15:46
  • For question 4-2, I mean that "this" is pointing to a component itself, assigned the value in a component constructor function, as this.wrappedJSObject = this; So the answer should be, even though the function which is defined in a component is calling in sandbox, its global scope of that function will be in the component itself but not the sandbox, which also prove that the global scope of a js function is established when it's defined but not when it's called. – winterTTr Mar 07 '11 at 01:55
0
  1. The scope is established when the function is defined.
  2. Sorry, don't know of any.
  3. The scope depends on what you are overlaying. If two components are overlaying the same element, they have the same scope.
  4. The global scope will be the one in which it was defined.
Sean Fujiwara
  • 4,506
  • 22
  • 34
  • Thanks a lot, if the js function global scope is established when the function is defined, i can understand a lot. And I means a XPCOM components which do not overlay anything. So, how about the question 3? – winterTTr Mar 04 '11 at 08:58
  • Hmm, it looks like you have to access the variables in your component with wrappedJSObject. That suggests the entire scope is isolated, so your component has it's own global object. I'm just guessing though, I haven't tested. – Sean Fujiwara Mar 04 '11 at 10:03
  • Components don't overlay anything, so your third answer is a little odd. – sdwilsh Mar 04 '11 at 14:26