Re: How to return values from a java script function

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"Stefan Mueller" <seekware-remove-@xxxxxxxxx> wrote in message
news:e68nuf$rfc$1@xxxxxxxxxxxxxx
I'd like to write a function 'double_values' which doubles two values.

var value1 = 5;
var value2 = 20;

alert("1) Value 1: " + value1 + " / Value 2: " + value2);
double_values(value1, value2);
alert("4) Value 1: " + value1 + " / Value 2: " + value2);

...

//-------------------------------------------------------

function double_values(a, b) {
alert("2) A: " + a + " / B: " + b);
a = a * 2;
b = b * 2;
alert("3) A: " + a + " / B: " + b);
}


Output:
1) Value 1: 5 / Value 2: 20
2) A: 5 / B: 20
3) A: 10 / B: 40
4) Value 1: 5 / Value 2: 20


I'm wondering why the output 4) shows
4) Value 1: 5 / Value 2: 20
and not
4) Value 1: 10 / Value 2: 40

What do I have to do to return both values a & b to value1 & value2?
Stefan



A function such as this should:

1) Not modify it's inputs
2) Not modify values external to it.

Hence the solution is to use an object similar to Daniel's post but should
return such a new instance of the object rather than modify the input
object.

function double_values(vals)
{
var ret = {}
for (var key in vals)
ret[key] = vals[key] * 2;

return ret
}

Anthony.


.



Relevant Pages

  • Re: accesing to the hover style with javascript
    ... u can bothe add a css rule of modify an existing one. ... the selector like: ... var style=document.createElement; ...
    (comp.lang.javascript)
  • Re: accesing to the hover style with javascript
    ... u can bothe add a css rule of modify an existing one. ... the selector like: ... var style=document.createElement; ...
    (comp.lang.javascript)
  • Re: Switching between different fonts...
    ... <div id='M' name='M'...). ... you assign a class and modify it's className property: ... Search the archives for some examples of modifying the className property. ... var newfamily; ...
    (comp.lang.javascript)
  • Re: var or inout parm?
    ... On Mon, Dec 8, 2008 at 12:26 AM, Bruno Desthuilliers ... How can I make a "var" parm, where the called function can modify ...
    (comp.lang.python)
  • Re: Evaulate an arithmetic expression
    ... Suppose the value of VAR is X, and the value of VAL is 5. ... this will just set VAR to 5, rather than modify X ... (setf (symbol-value var) ... explicitly declare any special variables it uses. ...
    (comp.lang.lisp)