By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,598 Members | 1,894 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,598 IT Pros & Developers. It's quick & easy.

Call by Value

P: n/a
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

Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Mario Thiel" <se*****@freenet.de> wrote in message
news:br*************@ID-217726.news.uni-berlin.de...
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>
Jul 20 '05 #2

P: n/a
@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+'<hr><h2>'+xy+'</h2');
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('<p align=center>--- End of this test -----<p>');
}
</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
document.write('<h1>Other Number</h1>');
Werf('15'); // Play with number 15
//Why cant i put the document.write in here?
</script> </body>
</html>


--
************************************************** ************
Stéphane MORIAUX : mailto:st*********************@wanadoo.fr
Aide aux Pages Perso (images & couleurs, formulaire, CHP, JS)
http://perso.wanadoo.fr/stephanemoriaux/internet/
************************************************** ************
Jul 20 '05 #3

P: n/a
Thanks you too, i see the mistake now. This script was just for
understanding, not for having any purpose.

Jul 20 '05 #4

P: n/a
Mario Thiel wrote:
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::o = " + 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
Jul 20 '05 #5

P: n/a
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
Mario Thiel wrote:
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:


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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.