Exception: TypeError: redeclaration of …

今天看了一篇關於web前端的文章。http://www.codeceo.com/article/full-frontend-guidelines.html 看見其中的js性能例子,感受很新奇,就試了一下。
// bad (albeit way faster)
const arr = [1, 2, 3, 4];
const len = arr.length;
var i = -1;
var result = [];
while (++i < len) {
var n = arr[i];
if (n % 2 > 0) continue;
result.push(n * n);
}
 
// good
const arr = [1, 2, 3, 4];
const isEven = n => n % 2 == 0;
const square = n => n * n;
 
const result = arr.filter(isEven).map(square);

爲了快速上手這個列子,咱們打開FireFox的代碼速記工具Scratchpad(在開發者工具裏面,快捷鍵 shift+F4).html

image

ctrl+R運行後報錯誤:TypeError:redeclaration of … 前端

在這個網站,給了我一個提示,原來須要使用嚴格模式。http://stackoverflow.com/questions/22603078/syntaxerror-use-of-const-in-strict-modeweb

在前面加上 'use strict' 就能解決此問題。app

image

相關文章
相關標籤/搜索