Closed Thread Icon

Topic awaiting preservation: Variable question, please. (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8800" title="Pages that link to Topic awaiting preservation: Variable question, please. (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Variable question, please. <span class="small">(Page 1 of 1)</span>\

 
Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 08-05-2003 01:56

Hi, guys. How are you?
Well, could I ask you about this, please?
I don't know why this code doesn't work.

code:
<script type="text/javascript">
function cal(){

Light = Math.pow(10,8);
var Year = form1.year.value;
var Kilo = form1.kilo.value;

Kilo = 3*Light*60*60*24*365/1000*Year;
}
</script>



I modified this to:

code:
<script type="text/javascript">
function cal(){

Light = Math.pow(10,8);


form1.kilo.value = 3*Light*60*60*24*365/1000*form1.year.value;
}
</script>



Then it seems to work fine. But I think the first code should work as well.
Would you see anything wrong??? When I run the first code in the IE5, nothing happens.
Even no error message either. Strange....

Hiroki Kozai

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 08-05-2003 02:06

The reason the first code doesn't work is that, when you assign Kilo to form1.kilo.value, the value of the form input is *copied*, and put into the Kilo variable. The Kilo variable is now completely separate from the form input. You can set Kilo to whatever you want, and the form input will no longer change.

This is called "copied by value," since the value of the first variable (form1.kilo.value) was copied into the second variable (Kilo).

What you were expecting is called "copy by reference", where the second variable is actually the *same thing* as the first variable. If Kilo was a *reference* to form1.kilo.value, that code would work, because assigning to Kilo would be the same as assigning to form1.kilo.value.

Unfortunately, in JavaScript, there's no way to copy a simple thing like a number or a string by reference. However, *objects* in JavaScript are always copied by reference. An object is anything with sub-variables, which are called "properties." For instance, form1 is an object, and one if its properties is kilo, which you can access via "form1.kilo". form1.kilo is also an object, which has the property "value", which you can access via "form1.kilo.value". However, form1.kilo.value is nothing more than a string, which contains the value of the form input that form1.kilo corresponds to.

You could do this:

var Kilo = form1.kilo
Kilo.value = ...;

And that would work fine. Why? Because form1.kilo, being an object, is copied by reference, so Kilo is now just another name for the same object. Therefore, it also has the value property, and when you assign to it, it's just like assigning to form1.kilo.value.

Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 08-06-2003 06:21

I have been reading your explanation, Slime.
Thanks a lot.
It takes time to sink into my mind.
Probably I will be back soon.
Again, thanks a lot for your kindness.
cya.

Hiroki Kozai

« BackwardsOnwards »

Show Forum Drop Down Menu