The short answer, as Greg Hewgill commented, is: no (to the best of my knowledge). There are a few shoddy ways you could get that behavior as well as a few recommended ways:
Shoddy Way #1:
One way is to have two versions of the function: one that returns r
, the other that returns k
. This is not recommended because it is duplicating the same code, just returning something different.
Shoddy Way #2:
Another option would be to add another parameter to the function which would specify the variable you want to be returned:
milkmachine = function(argument, retWhat){
var r;
var k;
//do something with arguments and variables
if (retWhat == "k")
return k;
else
return r;
}
This is also bad since you're using one function to do multiple things and a function should only do one thing; also, it gives external code more info about the inner workings of the function than is good. (There are probably other, better reasons this is bad, but I can't think of any at the moment.)
Recommended Way #1:
Instead of returning just one value, you could return both values wrapped in a container object:
milkmachine = function(argument, retWhat){
var r;
var k;
//do something with arguments and variables
return new MilkMachineRetrunValue(r, k);
}
var retval = milkmachine("some arg");
// access retval.r here
// access retval.k here
// or you could directly access the variable
// that you wanted within the return wrapper
// after the call like so:
// milkmachine("some arg").k
Recommended Way #2
Use an object:
function MilkMachine() {
this.r;
this.k;
}
MilkMachine.prototype.run = function(argument) {
//do something with arguments and variables
}
var mm = new MilkMachine();
mm.run("some arg");
// access mm.r here
// access mm.k here
return
statement will return a value. You access that value later by storing it into a variable so that you can refer to it later. – Robert Harvey Oct 31 '13 at 19:38k
instead of what's the internal variabler
as it was defined to return? – paul Oct 31 '13 at 19:46