Please confirm my suspicions on variable ordering

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fabian

    #1

    Please confirm my suspicions on variable ordering

    With teh following code, if I put picnames first, it wont work because
    the other variables havent yet been defined, correct? That seems to be
    the case. What I want to know is whether that is a feature or a bug.




    var r1 = new Array(
    "Anna",
    "Jane",
    "Hannah",
    "Nayomi"
    );
    var r2 = new Array(
    "My name is ",
    "I'm "
    );
    //
    ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~
    var d1 = r1[Math.floor(Math .random() * 4)];
    var d2 = r2[Math.floor(Math .random() * 2)];
    //
    ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~
    var picnames = new Array(
    "Good morning.",
    "Good morning.",
    "What's your name?",
    d2 + d1 + ".",
    "Thank you.",
    "You're welcome."
    );




    --
    --
    Fabian
    Visit my website often and for long periods!


  • Lee

    #2
    Re: Please confirm my suspicions on variable ordering

    Fabian said:[color=blue]
    >
    >With teh following code, if I put picnames first, it wont work because
    >the other variables havent yet been defined, correct? That seems to be
    >the case. What I want to know is whether that is a feature or a bug.[/color]
    [color=blue]
    >var picnames = new Array(
    > "Good morning.",
    > "Good morning.",
    > "What's your name?",
    > d2 + d1 + ".",
    > "Thank you.",
    > "You're welcome."
    >);[/color]

    It's a very important feature. When picnames is created,
    the value of the fourth element is determined by evaluating
    the current value of d1 and d2. If the values of d1 and d2
    change, the value of the fourth element won't change.
    That's the way you'll find that you almost always want it to
    work.

    If you want the fourth element of picnames to change when
    you change d1 and d2, just change it at the same time.

    Comment

    • Dr John Stockton

      #3
      Re: Please confirm my suspicions on variable ordering

      JRS: In article <bn9kq0$vcei7$2 @ID-174912.news.uni-berlin.de>, seen in
      news:comp.lang. javascript, Fabian <lajzar@hotmail .com> posted at Fri, 24
      Oct 2003 07:31:13 :-
      [color=blue]
      >With teh following code, if I put picnames first, it wont work because
      >the other variables havent yet been defined, correct? That seems to be
      >the case. What I want to know is whether that is a feature or a bug.[/color]

      It is neither; it is a necessity.

      The interpreter reads the code in order, executing what it finds; and
      must already know about any variables on the right of an assignment.
      After page load, functions may be called, and again all values needed
      must be pre-defined, in terms of the chronological sequence of
      execution.

      Note : function X(Y) { Z } is a definition; the function is not
      thereby executed, and the RHS may contain unknowns. In effect, the
      function text is just remembered.

      That's simplified.

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
      <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

      Comment

      Working...