What does the word "in" mean in the argument list of a method in the documentation?

Sorry for the possible stupid question, but I ask you to be lenient, because I just do not like to leave some spaces when I read documents.

Consider the following snippet from developer.mozilla.org :

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

As far as I know, there is no such syntax in JavaScript like void . But it’s just an agreement to say that the method returns nothing. (Please correct me if I am wrong).

But what does this mean in ?

+4
source share
1 answer

This means that the parameter is "in-parameter", that is, the value goes into the function, unlike "out-parameters", where the function adds data to this argument or this location in memory, which can then be used further by the calling code.

Here is an example from the MDN documentation containing "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 apparently they are more common in C or C ++, which is the language of Firefox.

As cPu1 pointed out , this is not JavaScript syntax, it is just 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 . The out parameter is essentially an auxiliary return value, although they are moderately cumbersome to use from script contexts, and should therefore be avoided if appropriate. The inout is a parameter whose value can be changed as a result of the method; these options are quite unpleasant to use and should generally be avoided, if at all possible.


You are right, void means that the function returns nothing, but the void operator exists (although this has nothing to do with the description of the interface).

+4
source

Source: https://habr.com/ru/post/1486891/


All Articles