views:

45

answers:

1

I'm not finding what's the type of this "xpcomInterface", and there's no documentation of any class with this name. Any idea?

This snippet is from Mozilla's website:

var next = elements.item(i+1);
var xpcomInterface = scroll.boxObject.QueryInterface(
Components.interfaces.nsIScrollBoxObject);
xpcomInterface.ensureElementIsVisible(elements);

--update

I found boxObject that leads to nslBoxObject, but it have no reference to any QueryInterface as used above. There's also references to xulplanet.com which is no more there.

+2  A: 

Well, xpcomInterface is just the name of the variable.

You should read about XPCOM and XPCOM interfaces. QueryInterface() is a method that all XPCOM objects must implement and gives you the possibility to "cast" an object to a certain interface:

(...) The function QueryInterface() is a function provided by all components which can be used to get a specific interface of that component. This function takes one parameter, the interface that you want to get. (...)

In this example, there is some object scroll.boxObject (update: which is a nsIBoxObject as you found out. Note that this again is just an interface (starts with nsI)) that seems to implement the nsIScrollBoxObject interface. By using QueryInterface, you can access those interface's methods like ensureElementIsVisible.

Felix Kling
strange, if javascript does not specify the type, why this QueryInterface exists? looks like a unneeded casting for me..
Tom Brito
Because those XPCOM components are not necessarily JavaScript objects: *XPCOM is a cross platform component object model, similar to Microsoft COM. It has multiple language bindings, letting the XPCOM components be used and implemented in JavaScript, Java, and Python in addition to C++.* https://developer.mozilla.org/en/XPCOM As far as I understood it, it is more like a bridge between several languages.
Felix Kling
It's not "casting" in the traditional sense - it's more that it's getting a "component" of that object (see the docs Felix mentioned). In your case think of it as doing something like the following "for scroll.boxObject, get the interface it implements that's called 'nsIScrollBoxObject'.
pnomolos