Question

Sorry for possibly stupid question but I ask you to be lenient, cause I simply do not like to leave some gaps when I read docs.

Consider the following snippet from developer.mozilla.org:

void initCustomEvent(
    in DOMString type,
    in boolean canBubble,
    in boolean cancelable,
    in any detail
);

As far ad I know, there is no such syntax in JavaScript like void. But this is simply a convention to tell, that the method does not return anything. (Please correct me if I'm wrong).

But what does it mean in ?

Was it helpful?

Solution

It mean the parameter is an "in-parameter", i.e. the value goes into the function, in contrast to "out-parameters" where the function adds data to that argument or that location in memory, which can then be used further by the calling code.

Here is an example from the MDN documentation containing an "out-parameter":

void get(
  in string prop,
  in nsIIDRef iid,
  [iid_is(iid),retval] out nsQIResult result
);

And a JavaScript example:

function range(n, result) {
    for (var i = 0; i < n; i++) {
        result.push(i);
    }
}

// to be called as
var foo = [];
range(10, foo);
console.log(foo); // shows [0,1,2,3,4,5,6,7,8,9]

"out-parameters" are not popular in JavaScript, but seem to be more common in C or C++, which is the language Firefox is developed in.

As hinted by c.P.u1, this is not JavaScript syntax, this is simply a description of the interface. Mozilla developed and used XPIDL. From their documentation:

Each method parameter can be specified in one of three modes: in, out, or inout. An out parameter is essentially an auxiliary return value, although these are moderately cumbersome to use from script contexts and should therefore be avoided if reasonable. An inout parameter is an in parameter whose value may be changed as a result of the method; these parameters are rather annoying to use and should generally be avoided if at all possible.


You are right, void means that the function does not return anything, but the void operator does exist (though it has nothing to do with the interface description).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top