0
function gResults () {
    var TestVar = myform.inputbox.value;
    var url = TestVar;
    document.myform.reset();
        window.location=TestVar;
}

Assume a text box with a url in it.

Will that function always grab the form's input? Are the function's list of things always done in order and saved in memory? Or will the window.location always go to null?

davin
  • 44,863
  • 9
  • 78
  • 78
Dave-o
  • 1
  • The code of a function is always executed line by line in order (assuming there are no control statements that direct it to do otherwise, like `if` and what not) – davin May 15 '11 at 18:11

2 Answers2

0

url will get the value of TestVar at the time the line was executed, i.e. before the call to reset(), see this example.

But why would you reset the form if you're redirecting the page anyway?

n0nick
  • 1,067
  • 10
  • 21
  • I'm actually doing other stuff. But just wanted to learn if the value would be reset and therefore kill the var value before it could use the value. – Dave-o May 15 '11 at 19:44
0

Strings are pass by value. That script would correctly set window.location to equal myForm.inputbox.value from before it was reset.

GAgnew
  • 3,847
  • 3
  • 26
  • 28
  • Everything in javascript is pass-by-value (yes everything), although your post implies otherwise... – davin May 15 '11 at 18:14
  • To expand on davin's comment, remember that when you pass a reference to an object into a function the reference is passed by value but the function can use that reference to add/modify/delete properties of the object it points to. (Not sure what pass-by-value or not has to do with the OP?) – nnnnnn May 16 '11 at 03:57
  • @davin You are wrong. Objects and arrays are both passed by reference, not value. @nnnnnn Uhm how about because that is what the question is about? if a var will or will not hold its value.. – GAgnew May 17 '11 at 20:46
  • @Greg, no you are wrong. I thought by including the extra brackets that I wouldn't get comments like these, although apparently that wasn't enough. JS is pass by value, as nnnnnn explained, when passing an object (an array is an object, not sure why you mention it like it's something separate), you pass the reference to the object. – davin May 17 '11 at 20:59
  • One (of many) references: http://stackoverflow.com/questions/518000/is-javascript-is-a-pass-by-reference-or-pass-by-value-language – davin May 17 '11 at 21:00
  • I'm going to try and be more polite here. First of all, 'passing' does not mean calling a function with the object as a parameter. It means any assignment. I quote you: 'you pass the reference to the object', and I completely agree. This is the very definition of PASS-BY-REFERENCE. In any programming language, passing by reference means instead of creating a new copy in memory of an object or datatype, it simply passes a pointer to the original object. Yes, your right that technically the object itself was only ever a reference to begin with, but there is a big difference between the two types – GAgnew May 17 '11 at 21:57
  • The first being in this scenario with pass by value: x = 'a'; b = x; b = 'c'; Here already I have demonstrated that the value passed is a new copy in memory, since b now equals c, and x still equals a. However, you will note in the case that: x = { a : 'b' }; c = x; c.a = 'd'; x.a will be changed such that x.a == c.a ; This is because both x and c are pointers to an original object. This gets more obvious when you add a third, or fourth reference. Effecting each individual copy will effect all of them. IE: Pass by reference simply means the value your are passing IS a reference. – GAgnew May 17 '11 at 22:04
  • @Greg, I will provide another very comprehensive link that explains that JS is **not** call by reference. I suggest you read this link and the previous ones. They explain in great detail while what you're saying is wrong. http://dmitrysoshnikov.com/ecmascript/chapter-8-evaluation-strategy/ – davin May 18 '11 at 12:58
  • By the way, if you are still too stubborn to realise that your view is misguided, I suggest (1) you read the code of some JS interpreter implementations or – davin May 18 '11 at 13:06
  • (2) prove this to yourself by analysing the memory footprint of a simple bit of code: `var base = "very long string", copy1 = base, copy2 = base, ...` According to your theory the assignment is by value, so a new string is copied for each instance. If your initial string is long enough you will see after a thousand copies that the memory footprint is proportional to the number of copies * size of the string. What actually happens is copying an address, so when I execute this code the footprint is very small, and not proportional to the size of the initial string, just the number of copies. – davin May 18 '11 at 13:07
  • I just ran this exact experiment on node and the difference in the memory footprint where one string was 2 characters long and another 3002 characters long, should theoretically be at least 1000 * 3000 bytes (all were ascii chars), which is almost 3Mb, but in reality the difference was 4kb (about 1/1000 of the expected value), which as I mentioned is proportional to the length of the longer string, *not multiplied by 1000*. Please read the linked material before continuing commenting. What I'm saying is not my personal opinion, it is that of the creators of the language. *Sigh*. – davin May 18 '11 at 13:27
  • I'm sorry what experiment? The article you linked LITERALLY HAS A SECTION TITLE 'CALL BY REFRENECE'. Maybe you should read your own links.. ALL LANGUAGES ARE TECHNICALLY ONLY PASS BY VALUE. Its IMPOSSIBLE to pass a reference with out passing a value (since the reference is always it-self a value.) – GAgnew May 18 '11 at 14:25
  • Pass-by-reference is an EXTENSION on pass-by-value NOT a mutually exclusive concept. And, even if it wasn't, you would take a language that you admit entirely acts like p-b-r and then go around telling everyone it was p-b-v for what reason? To confuse everyone? Computer Science is a constantly changing language, more so then any other language. What I'm trying to say, is how javascript handles objects falls under the term 'pass-by-reference' in any dictionary. Standford U PBR: 'Method of parameter passing: pass the L-value (address) of actual parameter. Allows to change the actual parameter.' – GAgnew May 18 '11 at 14:31
  • If you read the section on pass-by-reference you'll notice the code there is entitled "pseudo-code" since it isn't javascript as the author explains, and why that technique is not the same as the evaluation strategy of js. The experiment I was referring to was the string copying code, which you claimed was performed pass-by-value and the string value was copied. It doesn't really decide whether the language is or isn't pass-by-reference, although it refutes your concept of string assignment. – davin May 18 '11 at 14:34
  • "In most cases, for example, in Java, ECMAScript or Visual Basic, this strategy is also named as by value, meaning specific value — a reference copy. On the one hand, it is true — assigning to argument inside a function only binds this name with new value (address) and does not influence external object. On the other hand, such terminology crossing can really (if not to examine this question deeply) be treated incorrectly (what led to debate at forums how objects are passed to functions in JavaScript)." – GAgnew May 18 '11 at 14:45
  • This discussion is really of little further interest to me. If you disagree with me, that's ok, you can believe whatever makes you sleep better at night. Although when what you say is refuted by running tests (again, I urge you to test the string example and if you see different results, I'm all ears. You said that string assignment "is a new copy in memory" which it clearly isn't) then I wonder how you sleep. But like I said, whatever makes you feel good. Over and out. – davin May 18 '11 at 14:51