Call by Value

M

Mario Thiel

Hello,

i am new to JavaScript and right now i am learning Call by Value. i
tryed to write a small script but somehow it doesnt work right. i cant
put <br> into it, and i can only put the document.write(xyz,x) in the
function.

<html>
<head>
<script language="JavaScript">
function Wert(x) {
x += 5;
document.write(xyz)
document.write(x)//Why can i add an <br>?
}
</script>
</head>
<body>
<script language="JavaScript">
var xyz = 10;
Wert(xyz);
Wert(x);
//Why cant i put the document.write in here?
</script>
</body>
</html>

Greets from Germany
 
B

Brian

Mario Thiel said:
Hello,

i am new to JavaScript and right now i am learning Call by Value. i
tryed to write a small script but somehow it doesnt work right. i cant
put <br> into it, and i can only put the document.write(xyz,x) in the
function.

<html>
<head>
<script language="JavaScript">
function Wert(x) {
x += 5;
document.write(xyz)
document.write(x)//Why can i add an <br>?
}
</script>
</head>
<body>
<script language="JavaScript">
var xyz = 10;
Wert(xyz);
Wert(x);
//Why cant i put the document.write in here?
</script>
</body>
</html>

Greets from Germany

You are having a huge problem with variable scope. For instance, when you
call Wert(x), X has never been declared. Also, this code never tries to
write a <BR>. Basically, when you call Wert(x), you fail. Whatever is
happening, you are likely failing with something, which is likely due to
variable scope, and you are not finishing.

Both xyz and x need to be in global scope. I have no idea what this code is
supposed to due, but the following modifications will make everything work:

<html>
<head>
<script language="JavaScript">

// Declare the variables as global, before they are used.
var xyz = 10;
var x = 0;

function Wert(x) {
x += 5;
document.write(xyz)
document.write(x)//Why can i add an <br>?
document.write("<BR>");
}

</script>
</head>
<body>
<script language="JavaScript">
Wert(xyz);
Wert(x);
//Why cant i put the document.write in here?
// Wert(x) will not fail any more, allowing you to proceed
document.write("WRITE IS WORKING, now");
</script>
</body>
</html>
 
@

@SM

Mario Thiel a ecrit :
Hello,

i am new to JavaScript and right now i am learning Call by Value. i
tryed to write a small script but somehow it doesnt work right. i cant
put <br> into it, and i can only put the document.write(xyz,x) in the
function.

try that to understand a little
<html>
<head>
<script language="JavaScript">
function Wert(x) {

x=x*1; // to be sure x is a number
x += 5;
/*

document.write(xyz)

that is not correct ==> xyz is unknown
you could do : */
xy = 'hello world'
document.write(x+' said:
document.write('test with : '+x)//Why can i add an <br>?

// because you have to tell it in JS ==> write('<br>');

document.write('<hr><h3>Adding 5 times 1 to number : '+x+'</h3>');
for(var i=1;i<6;i++)
document.write((x*1+i*1)+'<br>');

document.write('<hr><h3>Geometrical suite by 20 of number : '+x+'</h3>');
for(var i=0;i<150;i+=20)
if(i>0)
document.write((x*i)+' = '+x+' * '+i+'<br>');

document.write(' said:
}
</script>
</head>
<body>
<script language="JavaScript">
var xyz = 10;
Wert(xyz);
/*

Wert(x);

*/
// you have to give a value to 'x'
Werf(4); // Play with number 4
//Why cant i put the document.write in here?
</script>
</body>
</html>

--
**************************************************************
Stéphane MORIAUX : mailto:[email protected]
Aide aux Pages Perso (images & couleurs, formulaire, CHP, JS)
http://perso.wanadoo.fr/stephanemoriaux/internet/
**************************************************************
 
M

Mario Thiel

Thanks you too, i see the mistake now. This script was just for
understanding, not for having any purpose.
 
T

Thomas 'PointedEars' Lahn

Mario said:
i am new to JavaScript and right now i am learning Call by Value.

JavaScript does not support call-by-value. It supports a special
kind of call-by-reference which differs from that you may be used
to:

function foobar(o, o2, p, i)
{
// Does not redefine the referenced object
// but uses `o' as local variable and assign
// it a primitive boolean value:
// "call-by-value"
o = false;
alert("foobar::eek: = " + o) // false

// Changes the value of a property of the referenced object
// using the value of `i'
// "call-by-reference" for one, "call-by-value" for the other
o2.haha = i;

// Adds a property to the referenced object:
// "call-by-reference"
o2.bla = "blurb";

// Does not change the value of the global variable but uses
// `p' as local variable:
// "call-by-value"
p = Math.PI;
}

var x = 42;
var obj1 = {};
var obj2 = {};
obj2.haha = "333";

foobar(obj1, obj2, x, 999);

// "42 [object Object] 999 blurb undefined" (Mozilla/5.0)
alert(
x
+ " " + obj1
+ " " + obj2.haha
+ " " + obj2.bla
+ " " + typeof o);


PointedEars
 
L

Lasse Reichstein Nielsen

Thomas 'PointedEars' Lahn said:
JavaScript does not support call-by-value. It supports a special
kind of call-by-reference which differs from that you may be used
to:

I guess that's a matter of philosophy. Javascript definitly passes
simple values (strings, numbers, booleans, null and undefined) by
value. The "problem" is objects, which are passed ... as objects.

I would still call it "call-by-value", it's just that the value of an
object is the object itself, not a copy of it (because objects have an
identity, an copy of an object would be a different object). It is
probably *implemente* by passing a reference, but it's not like you
can change what the reference points to (as with call-by-reference).

To me, Javascript is call-by-value (and so is, e.g., Java, but not
C# or C++)

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top