Introduction
When used as function arguments in JavaScript, number and string objects are compared by value, and functions are compared by reference... array objects (arrays) seem to be the only option for passing arguments by reference, so we'll simply wrap an array of size 1 into a new object class that sets/gets the first element:
function param()
{
this.array = new Array(1);
this.setValue = function(v) { this.array[0] = v; }
this.getValue = function() { return this.array[0]; }
}
It's a simple object with a 1-element internal array that will hold anything (even an object reference), exposing two simple assignment and retrieval methods.
You would typically use it in situations where you need more than one return value.
var s = new param;
var o = output(dText.innerHTML, s, 'Hello, world.');
function output(arg1, arg2, arg3)
{
...
arg2.setValue(a + b % 10);
...
}
dResult.innerHTML += s.getValue() + ' (' + o + ')';
And that's it. You can declare and pass-in as many parameters as you like, as long as your code handles them through the member functions; you can also use the internal array property if you need to handle the argument as an array, say to concatenate with other arrays, etc.
Why not use a one-sized array directly? Good, valid question. Actually, I just wanted to improve the code's readability, and this looked better than handling the 0th element around in functions. Maybe it's a waste of time, but hey, I'm into the "slow down" way of things now.
Professional software engineer with 30+ years of experience delivering systems across diverse industries, looking for the next opportunity to deliver cutting edge end-to-end technology solutions.
Avid reader, disciplined writer and enthusiastic tinkerer with a background in electronics, looking inside and thinking outside the box, genuinely passionate about robust, extensible, reusable and performant code.
Framework developer leading, coaching and learning about best practices, code quality, DevOps and software and data lifecycle management with an agile mindset to create the most elegant and sustainable solutions.