newbie: constants in JavaScript

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

    newbie: constants in JavaScript

    Hello,
    I am learning JavaScript and I have a question concerning constants in
    JavaScript:
    Why JSLint (http://www.jslint.com/) reports error on the following code:

    const reEmail = /^[a-z0-9-.]{1,40}@[a-z0-9-.]{1,70}$/;
    const reDate = /^(\d{4})(\/|-|\.)(\d{1,2})(\/|-|\.)(\d{1,2})$/;
    var error_message;
    var focus_on;
    function getElementValue (elem)
    {
    if (elem.tagName == 'input' && elem.length)
    for (var i = 0, len = elem.length; i < len; i++)
    if (elem[i].checked)
    {
    elem = elem[i];
    break;
    }
    return elem.value;
    }
    ....

    JSLint reports:

    Problem at line 1 character 1: Expected an identifier and instead
    saw 'const'.
    const reEmail = /^[a-z0-9-.]{1,40}@[a-z0-9-.]{1,70}$/;
    Problem at line 1 character 7: Stopping, unable to continue. (0% scanned).

    Is the syntax I use correct? If not, how to write it?

    Please help. Thank you!
  • Martin Honnen

    #2
    Re: newbie: constants in JavaScript

    Jivanmukta wrote:
    I am learning JavaScript and I have a question concerning constants in
    JavaScript:
    Why JSLint (http://www.jslint.com/) reports error on the following code:
    >
    const reEmail = /^[a-z0-9-.]{1,40}@[a-z0-9-.]{1,70}$/;
    If you want to learn and use Javascript as currently standardized,
    namely ECMAScript Edition 3, then you can't use const as that is not
    part of that standard. If you want to learn and use JavaScript as
    implemented by Mozilla's Spidermonkey or Rhino engine then you can use
    const. So it depends, if you want to write scripts for the web including
    browsers like IE then forget about const, you have only var. If you want
    to write Firefox extensions for instance, then you can use const.


    --

    Martin Honnen

    Comment

    • Jeremy J Starcher

      #3
      Re: newbie: constants in JavaScript

      On Sat, 08 Nov 2008 19:03:17 +0100, Jivanmukta wrote:
      Hello,
      I am learning JavaScript and I have a question concerning constants in
      JavaScript:
      >
      Why JSLint (http://www.jslint.com/) reports error on the following code:
      JSLint reports:
      Glad that you are using jslint. While it isn't perfect, it is a wonderful
      tool.
      Problem at line 1 character 1: Expected an identifier and instead saw
      'const'.
      The 'const' keyword is only known by a handful of emcascript
      interpreters. I *believe* that so far only the Javascript engine in
      Firefox supports it and *no* version of IE supports it.

      Just make it a regular variable.

      The jury is still out on if one should write it in ALLUPPERCASE or not.
      I tend to, but there was a recent discussion on that where some of the
      regulars opposed it.

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: newbie: constants in JavaScript

        Jivanmukta wrote:
        I am learning JavaScript and I have a question concerning constants in
        JavaScript:
        Why JSLint (http://www.jslint.com/) reports error on the following code:
        >
        const reEmail = /^[a-z0-9-.]{1,40}@[a-z0-9-.]{1,70}$/;
        [...]
        JSLint reports:
        >
        Problem at line 1 character 1: Expected an identifier and instead
        saw 'const'.
        const reEmail = /^[a-z0-9-.]{1,40}@[a-z0-9-.]{1,70}$/;
        Problem at line 1 character 7: Stopping, unable to continue. (0% scanned).
        >
        Is the syntax I use correct?
        That depends on which language you are talking about. JSLint, despite its
        name, appears to check strictly against the syntax rules of the language
        standard, ECMAScript; not JavaScript, the Netscape/Mozilla.org
        implementation of ECMAScript.

        Understand that you are not dealing with a single programming language
        here, but several versions of several similar languages instead. Currently
        two ECMAScript implementations , JavaScript and JScript, are compared against
        each other and against ECMAScript at <http://PointedEars.de/es-matrix>.
        More are yet to come.


        PointedEars
        --
        var bugRiddenCrashP ronePieceOfJunk = (
        navigator.userA gent.indexOf('M SIE 5') != -1
        && navigator.userA gent.indexOf('M ac') != -1
        ) // Plone, register_functi on.js:16

        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: newbie: constants in JavaScript

          Jeremy J Starcher <r3jjs@yahoo.co mwrites:
          The 'const' keyword is only known by a handful of emcascript
          interpreters. I *believe* that so far only the Javascript engine in
          Firefox supports it and *no* version of IE supports it.
          Actually, Opera, Safari and Google Chrome also support the keyword.
          Whether they also have the same semantics is always a good question
          for a non-standardized feature :)

          E.g.: the following program:
          const x;
          const y = x;
          x = 42;
          alert(x+y)
          is a syntax error in Opera, but yields NaN in the remaining browsers.
          And:
          var x = 21;
          const x = 42;
          alert(x+x)
          is an error in Safari and Firefox, but not in Opera or Chrome. Likewise
          is:
          const x = 42;
          var x = 21;
          alert(x+x)

          Just make it a regular variable.
          I agree. Const semantics is a mess to begin with.

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

          Comment

          • Martin Rinehart

            #6
            Re: newbie: constants in JavaScript



            Thomas 'PointedEars' Lahn wrote:
            Currently
            two ECMAScript implementations , JavaScript and JScript, are compared against
            each other and against ECMAScript at <http://PointedEars.de/es-matrix>.
            Superb table! Many thanks.

            Comment

            Working...