How to pass a variable by reference in javascript? Reading data from an ActiveX function that returns more than one value

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

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

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

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

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

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

+4
javascript activex
Nov 13 '09 at 23:32
source share
4 answers

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

+5
Nov 14 '09 at 8:04
source share

JavaScript does not support output parameter. Pack what you want to return in the automation object, assign values ​​to its properties and return it, or if your return value is already taken, create a class that has properties that you can assign in your ActiveX, and add a parameter whose type is class. In ActiveX, you can use IDispatch / Ex to get / set properties.

+2
Nov 14 '09 at 17:31
source share

Make a global variable or object. Or if you are worried that other functions are accessing and changing variables, then create a singleton. Another option is to return the object. Such as this

 function TOPOSFiscalPrinter.DirectIO(Command: Integer; var pData: Integer; var pString: WideString): Integer; function TOPOSFiscalPrinter.DirectIO(Command, pData, pString){ .... var pObj = { d: 0, s: '', code: '' } pObj.d = pDataAltertedValue; pObj.s = pStringAltertedValue; pObj.code = code; return pObj; } function test() { var d=1, s="DIRECIO:"; var r = opos.DirectIO(1024, d, s); code = r.code; d = rd; s = rs; alert(d); alert(s); } 
+1
Nov 17 '09 at 1:04
source share

Primitives like int or float always passed by value for performance reasons, but you can just wrap them, for example. a Float32Array with one element:

 a = new Float32Array([123]) a[0]; // == 123 function ChangeA(a) { a[0] = 333; } ChangeA(a) a[0]; // == 333 
0
May 09 '17 at 6:05 a.m.
source share



All Articles