Passing Parameters out by Reference?

Surely it's possible to pass parameters out of a function by reference, isn't it?

Example:  If select an item in my scene and run this:

(function() {	var oPrimarySel = null;		function doStuff(oNode) {		oNode = Scene.getPrimarySelection();	}	doStuff(oPrimarySel);	print('oPrimarySel = '+oPrimarySel);})()

I get "oPrimarySel = null"

Comments

  • Rob provides a link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions#Description and a quote

    "Arguments are passed to functions by value. If the function changes the value of an argument, this change is not reflected globally or in the calling function. However, object references are values, too, and they are special: if the function changes the referred object's properties, that change is visible outside the function..."

    you need the function to have a

    return oNode;

    line, and then the caller nees to assign the return value to a variable

    var oOutcome = doStuff(oPrimarySel);

  • Rob adds that if you created a new object and passed that, then you could store the result in a member of that object and it would be accessible (as I understand the process, thinking of C-type programming, you would pass a variable containing a pointer - the object pointed to is outside the scope of the function and can be changed in a pesistent way, but the pointer is still local to the function and changing that to point to a different object of the same type would not chnage the object available in the calling code).

Sign In or Register to comment.