Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass variable by reference in javascript? Read data from ActiveX function which returns more than one value

I have a ActiveX object which I want to use in the browser (javascript).
There is a function I want to call. Its prototype is:

function TOPOSFiscalPrinter.DirectIO(Command: Integer; var pData: Integer;
  var pString: WideString): Integer;

So, the function returns three values: result code, pData and pString;
In javascript the function does not update the variables pData and pString;

function test()
{
  var d=1, s="DIRECIO:";
  var code = opos.DirectIO(1024, d, s);

  alert(d); alert(s);
}

Variables d and s are not updated. They must be d=0 and s="ED123456";
How to read data from function which returns more than one value in javascript?

EDIT
Apparently, Javascript always passes parameters by value, never by reference.
Is there anything I can do to pass values by reference in Javascript, or I will have to change my design to only rely on parameters passed by value and on return values.

like image 853
Stanislav Stoyanov Avatar asked Nov 13 '09 23:11

Stanislav Stoyanov


People also ask

Does JavaScript always pass parameters by value or by reference select the most accurate answer?

JavaScript is always pass-by-value. This means everything in JavaScript is a value type and function arguments are always passed by value.

Which is better pass by value or pass by reference?

Pass-by-references is more efficient than pass-by-value, because it does not copy the arguments. The formal parameter is an alias for the argument. When the called function read or write the formal parameter, it is actually read or write the argument itself.

Does JavaScript always pass parameters by value or by reference?

Therefore, even changing the argument inside the function doesn't affect the variable passed from outside the function. It is important to note that in javascript, all function arguments are always passed by value. That is, JavaScript copies the values of the passing variables into arguments inside of the function.

How do I pass a variable from one script to another in JavaScript?

How do I pass a variable from one script to another in JavaScript? There are two ways to pass variables between web pages. The first method is to use sessionStorage, or localStorage. The second method is to use a query string with the URL.


1 Answers

Primitive types, primarily strings/numbers/booleans are passed by value for efficiency purposes. Objects such as functions, objects, arrays and the like are passed by reference. You can create an object and pass it, eg { d:1, s:'directo' } and then change the values because you're passing a reference.

like image 170
meder omuraliev Avatar answered Oct 19 '22 23:10

meder omuraliev