I want to create a string and pass it by reference such that I can change a single variable and have that propagate to any other object that references it.
Take this example:
function Report(a, b) { this.ShowMe = function() { alert(a + " of " + b); } } var metric = new String("count"); var a = new Report(metric, "a"); var b = new Report(metric, "b"); var c = new Report(metric, "c"); a.ShowMe(); // outputs: "count of a"; b.ShowMe(); // outputs: "count of b"; c.ShowMe(); // outputs: "count of c";
I want to be able to have this happen:
var metric = new String("count"); var a = new Report(metric, "a"); var b = new Report(metric, "b"); var c = new Report(metric, "c"); a.ShowMe(); // outputs: "count of a"; metric = new String("avg"); b.ShowMe(); // outputs: "avg of b"; c.ShowMe(); // outputs: "avg of c";
Why doesn't this work?
The MDC reference on strings says metric is an object.
I've tried this, which is not what I want, but is very close:
var metric = {toString:function(){ return "count";}}; var a = new Report(metric, "a"); var b = new Report(metric, "b"); var c = new Report(metric, "c"); a.ShowMe(); // outputs: "count of a"; metric.toString = function(){ return "avg";}; // notice I had to change the function b.ShowMe(); // outputs: "avg of b"; c.ShowMe(); // outputs: "avg of c"; alert(String(metric).charAt(1)); // notice I had to use the String constructor // I want to be able to call this: // metric.charAt(1)
The important points here:
Pass String by Reference in C++ The C++ reference is a name for a variable that already exists. A reference to a variable can't be altered to refer to the other variable once initialized. Pointers or references can be passed as parameters to functions in C++.
JavaScript is always pass-by-value. This means everything in JavaScript is a value type and function arguments are always passed by value. That being said, object types are a bit more confusing. The confusion lies in the fact that object types are reference types which are passed by value.
In JavaScript, all function arguments are always passed by value. It means that JavaScript copies the values of the variables into the function arguments. Any changes that you make to the arguments inside the function do not reflect the passing variables outside of the function.
Pass in Arguments by ReferenceNon-primitive are passed into a function by reference, which means that the reference to the object passed in as the argument is passed into the function. The copy of the content is not made for the argument and the passed in object is modified directly.
Strings in Javascript are already passed "by reference" -- calling a procedure with a string does not involve copying the string's contents. There are two issues at hand:
metric
is a label which applies to two entirely separate string variables.Here's one way to achieve what you want, using closures to implement dynamic scoping of metric
:
function Report(a, b) { this.ShowMe = function() { alert(a() + " of " + b); } } var metric = "count"; var metric_fnc = function() { return metric; } var a = new Report(metric_fnc, "a"); var b = new Report(metric_fnc, "b"); a.ShowMe(); // outputs: "count of a"; metric = "avg"; b.ShowMe(); // outputs: "avg of b";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With