var

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

    #1

    var

    function shownumber(val) {
    // XBM format
    var Head = "#define count_width 8\n"+
    "#define count_height 16\n"+
    "static char count_bits[] = {";
    var Count, dig, tag;

    Digit = new Array();
    Digit[0]=Head+"0xff,0xf f,0xff,0xc3,0x9 9,0x99,0x99,0x9 9,"+
    "0x99,0x99,0x99 ,0x99,0xc3,0xff ,0xff,0xff};";
    // ... sniped ....
    for (Count = 0; Count < val.length; Count++) {
    dig=val.substri ng(Count, Count+1);
    tag="<IMG SRC='JavaScript :Digit["+dig+"]' "+
    "HEIGHT=16 WIDTH=8 ALIGN='absmiddl e'>";
    document.write( tag);
    }
    }

    When I run shownumber("0") , an XBM would be disaplayed. But if I changed
    the declaration for the array to:

    var Digit = new Array();

    shownumber() would no longer work. Why? Everything is contained in the
    function. Why can't I use local variable for the array?



    --
    .~. Might, Courage, Vision. In Linux We Trust.
    / v \ http://www.linux-sxs.org
    /( _ )\ Linux 2.4.22-xfs
    ^ ^ 12:08am up 1 day 28 min load average: 1.00 1.00 1.00
  • toylet

    #2
    Re: var

    > tag="<IMG SRC='JavaScript :Digit["+dig+"]' "+[color=blue]
    > "HEIGHT=16 WIDTH=8 ALIGN='absmiddl e'>";
    > document.write( tag);
    > var Digit = new Array();
    > shownumber() would no longer work. Why? Everything is contained in the
    > function. Why can't I use local variable for the array?[/color]

    I think I knew the answer. Something to do with order of execution.

    What's the proper style for declaring global variable in Javascript?

    --
    .~. Might, Courage, Vision. In Linux We Trust.
    / v \ http://www.linux-sxs.org
    /( _ )\ Linux 2.4.22-xfs
    ^ ^ 12:14am up 1 day 34 min load average: 1.01 1.01 1.00

    Comment

    • Douglas Crockford

      #3
      Re: var

      > I think I knew the answer. Something to do with order of execution.[color=blue]
      >
      > What's the proper style for declaring global variable in Javascript?[/color]

      Generally, it is best to avoid global variables. When you cannot avoid
      them, they should be defined before they are used.


      Comment

      • toylet

        #4
        Re: var

        Douglas Crockford wrote:
        [color=blue][color=green]
        >> I think I knew the answer. Something to do with order of execution.
        >>
        >> What's the proper style for declaring global variable in Javascript?[/color]
        >
        > Generally, it is best to avoid global variables. When you cannot avoid
        > them, they should be defined before they are used.
        >
        > http://www.crockford.com/#javascript[/color]

        you meant something like this?

        <html><head>
        <script type="text/javascript">
        $global_var1 = new Array();
        $global_var2 = 1;
        $global_var3 = "";

        Function one() {}
        function two() {}

        // main
        window.onload = one;
        </script>
        </head><body>
        </body></html>

        --
        .~. Might, Courage, Vision. In Linux We Trust.
        / v \ http://www.linux-sxs.org
        /( _ )\ Linux 2.4.22-xfs
        ^ ^ 12:52am up 1 day 1:12 load average: 0.99 0.97 0.97

        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: var

          toylet <toylet_at_mail .hongkong.com> writes:
          [color=blue]
          > you meant something like this?[/color]

          (Missing DOCTYPE declration, required by HTML)[color=blue]
          > <html><head>[/color]
          (Missing title element, also required)[color=blue]
          > <script type="text/javascript">
          > $global_var1 = new Array();[/color]

          I would write

          var $global_var1 = new Array();

          Using the "var" declaration at the top level (not inside a function body)
          declares a global variable.

          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • Douglas Crockford

            #6
            Re: var

            >>> What's the proper style for declaring global variable in Javascript?
            [color=blue][color=green]
            >> Generally, it is best to avoid global variables. When you cannot avoid
            >> them, they should be defined before they are used.[/color][/color]
            [color=blue]
            > you meant something like this?[/color]
            [color=blue]
            > <html><head>
            > <script type="text/javascript">
            > $global_var1 = new Array();
            > $global_var2 = 1;
            > $global_var3 = "";
            >
            > Function one() {}
            > function two() {}
            >
            > // main
            > window.onload = one;
            > </script>
            > </head><body>
            > </body></html>[/color]

            Close. Leave off the type="text/javascript". That is a Microsoftism that
            didn't make it into the standard.

            I would write

            var global_var1 = [];
            var global_var2 = 1;
            var global_var3 = "";

            Use var to distinguish a new definition from using an existing
            definition. Avoid using '$'. It was intended for use by program
            generators. It looks ugly. Also, I prefer [] to new Array() .
            Watch the capitalization on 'function'. JavaScript is case sensitive.
            I would define function one this way:

            window.onload = function one() {};

            Finally, I recommend that you run your stuff through JSLint.


            Comment

            • toylet

              #7
              [OT] Re: var

              > (Missing DOCTYPE declration, required by HTML)[color=blue]
              > (Missing title element, also required)[/color]

              Not related to javascript, it seems that these 2 tags are not sondier
              important by most browsers. At least, the page could still be rendered.


              --
              .~. Might, Courage, Vision. In Linux We Trust.
              / v \ http://www.linux-sxs.org
              /( _ )\ Linux 2.4.22-xfs
              ^ ^ 1:28am up 1 day 1:48 load average: 1.00 1.00 1.00

              Comment

              • toylet

                #8
                Re: var

                > Close. Leave off the type="text/javascript". That is a Microsoftism that[color=blue]
                > didn't make it into the standard.[/color]

                what should it be? `lanaguage="jav ascript"`? Thanks.
                [color=blue]
                > definition. Avoid using '$'. It was intended for use by program[/color]

                Sorry, I mixed things up with php and perl.
                [color=blue]
                > Finally, I recommend that you run your stuff through JSLint.
                >
                > http://www.crockford.com/javascript/lint.html[/color]

                will try.


                --
                .~. Might, Courage, Vision. In Linux We Trust.
                / v \ http://www.linux-sxs.org
                /( _ )\ Linux 2.4.22-xfs
                ^ ^ 1:36am up 1 day 1:56 load average: 0.99 0.94 0.95

                Comment

                • Lasse Reichstein Nielsen

                  #9
                  Re: var

                  toylet <toylet_at_mail .hongkong.com> writes:

                  [about <script type="text/javascript">]
                  [color=blue][color=green]
                  >> Close. Leave off the type="text/javascript". That is a Microsoftism
                  >> that didn't make it into the standard.[/color]
                  >
                  > what should it be? `lanaguage="jav ascript"`? Thanks.[/color]

                  You should use 'type="text/javascript"'. I think Richard managed to
                  misread it somehow. It is not related to Microsoft at all, but comes
                  from the HTML specification.

                  The type attribute is required by HTML 4, and the content type
                  "text/javascript" is the suggested value to use for Javascript content
                  (even though it is not standardized as a MIME type).

                  <URL:http://www.w3.org/TR/html4/interact/scripts.html#h-18.2.1>

                  /L
                  --
                  Lasse Reichstein Nielsen - lrn@hotpop.com
                  DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                  'Faith without judgement merely degrades the spirit divine.'

                  Comment

                  • Lasse Reichstein Nielsen

                    #10
                    Re: var

                    Lasse Reichstein Nielsen <lrn@hotpop.com > writes:
                    [color=blue]
                    > I think Richard[/color]

                    Make that Douglas. So much for short term memeory (I remembered two
                    newsgroups later, though :)

                    /L
                    --
                    Lasse Reichstein Nielsen - lrn@hotpop.com
                    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                    'Faith without judgement merely degrades the spirit divine.'

                    Comment

                    Working...