What browsers currently support JavaScript's 'let' keyword?
I'm developing an app and don't have to ever worry about Internet Explorer and was looking into some of the features present in A+ grade browsers that aren't in Internet Explorer1.
One of these features I wanted to play around with is JavaScript's let keyword
I can't seem to get any of their 'let' examples to work in Firefox 3.6 (User-Agent string: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)). I get SyntaxError: missing ; before statement
SyntaxError: missing ; before statement
when executing let foo = "bar"
.
So, what browsers support the let keyword? (Or am I doing something wrong?)
Those extensions are not ECMA-Standard, they are supported only by the Mozilla implementation.
On browser environments you should include the JavaScript version number in your script
tag to use it:
<script type="application/javascript;version=1.7">
var x = 5;
var y = 0;
let (x = x+10, y = 12) {
alert(x+y + "n");
}
alert((x + y) + "n");
</script>
Internet Explorer≥11(仅for
范围内缺失)和所有当前浏览器(ECMAScript 6兼容性表:let)均提供完全支持。
As of April 2017:
All up-to-date major browsers such as Chrome, Firefox, and Edge support the ES2015 (aka "ES6") let
keyword.
iOS Safari did not support let
until OS 10 (eg, OS 9 did not).
Some older browsers, such as IE9-IE11, support an early version of let
but don't support the semantics defined by ES2015 (particularly in relation to declarations in the headers of for
loops). So it's not a syntax error, and it does declare the variable, but it doesn't work the way it's supposed to. For instance, in a correct implementation, the following logs 0, 1, and 2; on IE9-IE11, it logs 3, 3, 3:
for (let i = 0; i < 3; ++i) {
setTimeout(function() {
console.log(i);
}, i * 100);
}
链接地址: http://www.djcxy.com/p/1494.html
上一篇: JavaScript中的函数重载