...

View Full Version : "let" keyword not working



Dean440
06-15-2010, 04:37 PM
Here's some code copied directly from Mozilla:

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?

Dean440
06-15-2010, 04:51 PM
Sorry, silly me. You need to specify that you're using Javascript 1.7 in your opening script tag.

Philip M
06-15-2010, 05:17 PM
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.

Dean440
06-15-2010, 05:38 PM
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."

Philip M
06-15-2010, 05:49 PM
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.

Dean440
06-15-2010, 05:57 PM
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?

Philip M
06-15-2010, 06:02 PM
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.

Dean440
06-15-2010, 06:24 PM
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. :)

Skychan
06-15-2010, 06:51 PM
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?



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...

Skychan
06-15-2010, 07:00 PM
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...

Skychan
06-15-2010, 07:10 PM
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.

gsnedders
06-16-2010, 10:16 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum