最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Apr 19, 2012 at 15:54 Nathan Hughes 96.4k20 gold badges191 silver badges282 bronze badges asked Nov 13, 2009 at 23:32 Stanislav StoyanovStanislav Stoyanov 2,1202 gold badges20 silver badges22 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 8

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.

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

Primitives like int's or float's are always passed by value for performance reasons, but you can just wrap them into e.g. a Float32Array with just one element:

a = new Float32Array([123])
a[0]; // == 123
function ChangeA(a) {
    a[0] = 333;
}
ChangeA(a)
a[0]; // == 333

Make a global variable or object. Or if you're worried about other funcs accessing and changing the variables then make a singleton. The other option is to return an object. Such as like 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 = r.d;
    s = r.s;



    alert(d); alert(s);
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论