What does this mean

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

    #1

    What does this mean

    I am learning Javascript; and most books only give you partial
    definitions for the functions they show you. Here is a line of code
    from a browser sniffing function:

    var isWin = (navigator.plat form.indexOf("W in") !=-1) ? true:false;

    What does the -1 stand for.

    Excuse me for such a basic question, but I can't find the answer
    anyway.

    Thanks
  • Andrew Thompson

    #2
    Re: What does this mean

    On 17 Sep 2004 09:06:39 -0700, Kim Forbes wrote:
    [color=blue]
    > I am learning Javascript; and most books only give you partial
    > definitions for the functions they show you. Here is a line of code
    > from a browser sniffing function:[/color]

    Browser sniffing? If the book burns, it may
    be useful to keep you warm for a few minutes,
    otherwise it has no (good) use.

    Search the group for 'feature detection'. Once
    you have the hang of that, you will no longer
    need to worry about what browser your visitors
    are using.
    [color=blue]
    > var isWin = (navigator.plat form.indexOf("W in") !=-1) ? true:false;
    >
    > What does the -1 stand for.[/color]

    In the string 'AWindowsPC', 'Win' occurs..
    ^
    0123456789
    ...at index 1.

    In the string 'TheWindow', 'Win' occurs
    ^
    0123456789
    ...at index 3.

    ...but in the string "ThisIsAnAppleM acintosh",
    'Win' occurs ..nowhere. The function returns
    -1 to indicate that 'Win' was *not* contained
    in the string "ThisIsAnAppleM acintosh".

    ( ..but use feature detection, browser sniffing
    is pointless, stupid, and leads to fragile code. )

    HTH

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.lensescapes.com/ Images that escape the mundane

    Comment

    • Michael Winter

      #3
      Re: What does this mean

      On 17 Sep 2004 09:06:39 -0700, Kim Forbes <kim.forbes@tch .harvard.edu>
      wrote:
      [color=blue]
      > I am learning Javascript; and most books only give you partial
      > definitions for the functions they show you. Here is a line of code from
      > a browser sniffing function:
      >
      > var isWin = (navigator.plat form.indexOf("W in") !=-1) ? true:false;[/color]

      This could be written as

      var isWin = (navigator.plat form.indexOf('W in') != -1);

      The conditional statement is superfluous as the comparison already
      evaluates to a boolean. However, you should avoid this sort of thing (see
      below).
      [color=blue]
      > What does the -1 stand for.[/color]

      The String.prototyp e.indexOf method returns the index of the first
      occurance of the supplied string. As in most circumstances, indicies start
      at zero (0), so minus one (-1) is used to signify that the substring
      couldn't be found.

      It's important that you ignore anything regarding browser sniffing. It's a
      flawed technique that often results in scripts that break with unknown
      user agents, or those that spoof themselves as other browsers. Instead,
      you should learn about feature detection. See:

      <URL:http://jibbering.com/faq/#FAQ4_26>

      and its links.

      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      • Antonie C Malan Snr

        #4
        Re: What does this mean

        Kim Forbes wrote:[color=blue]
        > I am learning Javascript; and most books only give you partial
        > definitions for the functions they show you. Here is a line of code
        > from a browser sniffing function:
        >
        > var isWin = (navigator.plat form.indexOf("W in") !=-1) ? true:false;
        >
        > What does the -1 stand for.
        >
        > Excuse me for such a basic question, but I can't find the answer
        > anyway.
        >
        > Thanks[/color]

        You will only learn if you ask.

        isWin is a boolean with values only true or false.

        The above is a short way to write an if, else statement.

        The -1 means that the String "Win" is not present in the String returned
        by navigator.platf orm If it was the value would have been the first
        occurence of the "W" in "Win" starting to count from 0 in the String
        returned by navigator.platf orm. This short statement tests if the user
        uses any variant of Windows, in itself a bad thing to do. The
        intelligent user uses Linux, or maybe even a Mac.

        If you want to I can rewrite that into the mentioned if, else statement
        which is more easily understandable.

        Chris

        Comment

        • Kim Forbes

          #5
          Re: What does this mean

          Antonie C Malan Snr <malan2000@optu snet.com.au> wrote in message news:<414cfaaf$ 0$29780$afc38c8 7@news.optusnet .com.au>...[color=blue]
          > Kim Forbes wrote:[color=green]
          > > I am learning Javascript; and most books only give you partial
          > > definitions for the functions they show you. Here is a line of code
          > > from a browser sniffing function:
          > >
          > > var isWin = (navigator.plat form.indexOf("W in") !=-1) ? true:false;
          > >
          > > What does the -1 stand for.
          > >
          > > Excuse me for such a basic question, but I can't find the answer
          > > anyway.
          > >
          > > Thanks[/color]
          >
          > You will only learn if you ask.
          >
          > isWin is a boolean with values only true or false.
          >
          > The above is a short way to write an if, else statement.
          >
          > The -1 means that the String "Win" is not present in the String returned
          > by navigator.platf orm If it was the value would have been the first
          > occurence of the "W" in "Win" starting to count from 0 in the String
          > returned by navigator.platf orm. This short statement tests if the user
          > uses any variant of Windows, in itself a bad thing to do. The
          > intelligent user uses Linux, or maybe even a Mac.
          >
          > If you want to I can rewrite that into the mentioned if, else statement
          > which is more easily understandable.
          >
          > Chris[/color]

          Thanks for the answer. Actually, what I would like is a good reference
          book where I can learn Javascript inside out. I took a class about a
          year ago, but it didn't help me like I wanted. I found many mistakes
          in the textbook, and it wasn't as comprehensive as I wanted. What I
          want to know is what every line in the code I'm writing means, and why
          am I writing that way. As it is now, I can generally write enough code
          to get it to do what I want; but I'm not sure why it works like it
          does. I'm just parrotting what I learned.

          Comment

          • Kim Forbes

            #6
            Re: What does this mean

            Andrew Thompson <SeeMySites@www .invalid> wrote in message news:<14w0806mu z1t1$.1gu6byn55 lxod.dlg@40tude .net>...[color=blue]
            > On 17 Sep 2004 09:06:39 -0700, Kim Forbes wrote:
            >[color=green]
            > > I am learning Javascript; and most books only give you partial
            > > definitions for the functions they show you. Here is a line of code
            > > from a browser sniffing function:[/color]
            >
            > Browser sniffing? If the book burns, it may
            > be useful to keep you warm for a few minutes,
            > otherwise it has no (good) use.
            >
            > Search the group for 'feature detection'. Once
            > you have the hang of that, you will no longer
            > need to worry about what browser your visitors
            > are using.
            >[color=green]
            > > var isWin = (navigator.plat form.indexOf("W in") !=-1) ? true:false;
            > >
            > > What does the -1 stand for.[/color]
            >
            > In the string 'AWindowsPC', 'Win' occurs..
            > ^
            > 0123456789
            > ..at index 1.
            >
            > In the string 'TheWindow', 'Win' occurs
            > ^
            > 0123456789
            > ..at index 3.
            >
            > ..but in the string "ThisIsAnAppleM acintosh",
            > 'Win' occurs ..nowhere. The function returns
            > -1 to indicate that 'Win' was *not* contained
            > in the string "ThisIsAnAppleM acintosh".
            >
            > ( ..but use feature detection, browser sniffing
            > is pointless, stupid, and leads to fragile code. )
            >
            > HTH[/color]

            Thanks so much. That was exactly what I wanted to know. I started
            checking out feature detection, it seems that once I figure it out, it
            will be a lot easier to write and implement.

            Thanks again.

            Comment

            • Kim Forbes

              #7
              Re: What does this mean

              "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message news:<opsehcj7h nx13kvk@atlanti s>...[color=blue]
              > On 17 Sep 2004 09:06:39 -0700, Kim Forbes <kim.forbes@tch .harvard.edu>
              > wrote:
              >[color=green]
              > > I am learning Javascript; and most books only give you partial
              > > definitions for the functions they show you. Here is a line of code from
              > > a browser sniffing function:
              > >
              > > var isWin = (navigator.plat form.indexOf("W in") !=-1) ? true:false;[/color]
              >
              > This could be written as
              >
              > var isWin = (navigator.plat form.indexOf('W in') != -1);
              >
              > The conditional statement is superfluous as the comparison already
              > evaluates to a boolean. However, you should avoid this sort of thing (see
              > below).
              >[color=green]
              > > What does the -1 stand for.[/color]
              >
              > The String.prototyp e.indexOf method returns the index of the first
              > occurance of the supplied string. As in most circumstances, indicies start
              > at zero (0), so minus one (-1) is used to signify that the substring
              > couldn't be found.
              >
              > It's important that you ignore anything regarding browser sniffing. It's a
              > flawed technique that often results in scripts that break with unknown
              > user agents, or those that spoof themselves as other browsers. Instead,
              > you should learn about feature detection. See:
              >
              > <URL:http://jibbering.com/faq/#FAQ4_26>
              >
              > and its links.
              >
              > Mike[/color]

              Mike,
              I've bookmarked the URL. Thanks.

              Comment

              • Robert

                #8
                Re: What does this mean

                In article <de27664f.04092 00450.59ec03d5@ posting.google. com>,
                kim.forbes@tch. harvard.edu (Kim Forbes) wrote:
                [color=blue]
                > Thanks for the answer. Actually, what I would like is a good reference
                > book where I can learn Javascript inside out.[/color]

                This group recommends JavaScript: The Definitive Guide by David
                Flanagan.

                Robert

                Comment

                • Dr John Stockton

                  #9
                  Re: What does this mean

                  JRS: In article <rccharles-848DE4.15141520 092004@news2.we st.earthlink.n
                  et>, dated Mon, 20 Sep 2004 19:08:11, seen in news:comp.lang. javascript,
                  Robert <rccharles@my-deja.com> posted :[color=blue]
                  >In article <de27664f.04092 00450.59ec03d5@ posting.google. com>,
                  > kim.forbes@tch. harvard.edu (Kim Forbes) wrote:
                  >[color=green]
                  >> Thanks for the answer. Actually, what I would like is a good reference
                  >> book where I can learn Javascript inside out.[/color]
                  >
                  >This group recommends JavaScript: The Definitive Guide by David
                  >Flanagan.[/color]

                  AIUI, this group merely considers it to be the best available book.
                  That is not the same as saying that it will enable one to "learn
                  Javascript inside out". For that, one should read ECMA-262, and make
                  all possible deductions from it. One will, also, need to learn about
                  DOMs.


                  Learning Javascript inside out can only be possible using multiple
                  sources of information, and much practice.

                  --
                  © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                  <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
                  <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                  <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                  Comment

                  Working...