Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Dec 2009
    Posts
    115
    Thanks
    2
    Thanked 10 Times in 10 Posts

    "let" keyword not working

    Here's some code copied directly from Mozilla:
    Code:
    var x = 5;
    var y = 0;
    
    let (x = x+10, y = 12) {
      alert(x+y + "\n");
    }
    
    alert((x + y) + "\n");
    Both Chrome and Firefox display an error regarding the bolded line.

    Chrome says "unexpected token '{' " on that line and Firefox says "Missing ';' before statement" on that line.

    At any rate, it doesn't work. Is this not the correct syntax for let blocks? Has something changed since Mozilla wrote that example?

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Posts
    115
    Thanks
    2
    Thanked 10 Times in 10 Posts
    Sorry, silly me. You need to specify that you're using Javascript 1.7 in your opening script tag.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,247
    Thanks
    203
    Thanked 2,556 Times in 2,534 Posts
    Internet Explorer and Opera don't support let on any browser version. Firefox does support it since version 2.0 and Safari since 3.2. But IE and Opera together have over 60% of the browser market.

  • #4
    Regular Coder
    Join Date
    Dec 2009
    Posts
    115
    Thanks
    2
    Thanked 10 Times in 10 Posts
    Quote Originally Posted by Philip M View Post
    Internet Explorer and Opera don't support let on any browser version. Firefox does support it since version 2.0 and Safari since 3.2. But IE and Opera together have over 60% of the browser market.
    It's kind of silly to have so many concepts like "yield", "let", for ... in, and for each that are mainly only implemented by Mozilla.

    Correct me if I'm wrong here, but I get the feeling that the overall advice for use of these concepts is "Do not use these. They're not standard and won't work reliably."

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,247
    Thanks
    203
    Thanked 2,556 Times in 2,534 Posts
    Quote Originally Posted by Dean440 View Post
    It's kind of silly to have so many concepts like "yield", "let", for ... in, and for each that are mainly only implemented by Mozilla.

    Correct me if I'm wrong here, but I get the feeling that the overall advice for use of these concepts is "Do not use these. They're not standard and won't work reliably."
    Yes, that would be my advice. Of course, these things may be implemented in IE and Opera in the future, but you must still allow for older browsers. FWIIW I only write code which works in IE6 - lots of individuals still use it, especially people who I see as my market, that is older people.

  • #6
    Regular Coder
    Join Date
    Dec 2009
    Posts
    115
    Thanks
    2
    Thanked 10 Times in 10 Posts
    Quote Originally Posted by Philip M View Post
    Yes, that would be my advice. Of course, these things may be implemented in IE and Opera in the future, but you must still allow for older browsers. FWIIW I only write code which works in IE6 - lots of individuals still use it, especially people who I see as my market, that is older people.
    Would you say that Iterators and Generators should be avoided for now as well?

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,247
    Thanks
    203
    Thanked 2,556 Times in 2,534 Posts
    Quote Originally Posted by Dean440 View Post
    Would you say that Iterators and Generators should be avoided for now as well?
    Yes, definitely IMHO. These really do nothing that cannot be achieved by "ordinary" Javascript 1.5.
    Last edited by Philip M; 06-15-2010 at 06:05 PM.

  • #8
    Regular Coder
    Join Date
    Dec 2009
    Posts
    115
    Thanks
    2
    Thanked 10 Times in 10 Posts
    Quote Originally Posted by Philip M View Post
    Yes, definitely IMHO. These really do nothing that cannot be achieved by "ordinary" Javascript 1.5.
    Thanks for the sound advice. That clears up some confusion for me.

  • #9
    New Coder
    Join Date
    Feb 2010
    Posts
    44
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Woah, I didn't even know JS doesn't support block-level scoping!

    So the following code won't give different values to the anonymous function?

    Code:
    function foo()
    {
      for(var i = 0; i < 4; i++)
      {
        var iCopy = i;
        addEvent(function(){ alert(iCopy); });
      }
    }
    
    //later on the anonymous functions are ran... will my output be:
    //0,1,2,3
    //or
    //4,4,4,4
    //?
    I guess I could just go test this right now...

  • #10
    New Coder
    Join Date
    Feb 2010
    Posts
    44
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Ok, I tested it, I indeed get the output 3, 3, 3, 3 (obviously I was mistaken when I was 4,4,4,4)

    So ya, no block-level scoping!

    hmmm, I need to rethink how to pass the value of i to the anonymous function...

  • #11
    New Coder
    Join Date
    Feb 2010
    Posts
    44
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Code:
    function foo()
      for(var i = 0; i < 4; i++)
      {
        var func = function(iCopy){
          addEvent(function(){ alert(iCopy); });
        };
        func(i);
      }
    }
    That does it. I don't think there is any other way, short of passing i to addEvent() and expecting the code that runs events to pass the appropriate i to the appropriate event... which is of course possible, but not appropriate because not all events are going to expect a param.

  • #12
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    Quote Originally Posted by Philip M View Post
    Of course, these things may be implemented in IE and Opera in the future, but you must still allow for older browsers.
    To post something with my Opera-employee hat on for once: we're unlikely to implement non-standard extensions to ECMAScript, as we believe having open standards leads to be better, more open, web.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •