I’ve some scenarios where i need to pass value type as reference without changed the processing function.
-
Example Numeric Types (var limit)
var limit = 0; // Need to be the reference type var multiCallback = new MultiCallback(limit, function(){}); for (element in myObject) { limit++; element.DoSomething(multiCallback.callback); } function MultiCallback(limit, func) { var calls = 0; function callback() { if (++calls == limit) { func(); } } return { callback : callback } } -
Examble Function Types
var resizeCallback = function(){}; $(window).resize(resizeCallback); function showPage() { resizeCallback = resizePage(); } function showLoader() { resizeCallback = resizeLoader(); }
is there a solution
Changing the value of a variable will never update the previous value of the variable.
For functions, you can do something like this:
For other values (primitives and objects), the closest thing is to pass an object: