5
我剛剛發現了一個第三方wordpress插件中的一個bug,該插件看起來是由javascript代碼縮小器造成的。爲什麼JavaScript會爲++和字符串產生不同的錯誤?
原代碼,我認爲,應該是這樣的:
this.id = "ui-id-" + ++n;
相反,它已被精縮到:
this.id="ui-id-"+++n;
這導致在Chrome以下錯誤:
Uncaught ReferenceError: Invalid left-hand side expression in postfix operation
和Firefox中的類似錯誤。煩人的是,在Chrome中我自己的插件JavaScript函數仍然創建成功,但在Firefox中,這個錯誤導致我的函數不被創建,我的插件失敗。
var n = 1;
var foo = 10;
var bar = "ID-";
console.log(foo+++n); // results in 11
console.log(foo); // also results in 11
console.log(bar+++n); // results in NaN soft error/warning
console.log ("ID-"+ ++n); // results in ID-2
console.log ("ID-"+++n); // hard error
我真的不知道要問什麼問題在這裏 -
- 爲什麼+++總要得到解釋爲++ +?
- 哪些限制器會導致此錯誤?
- 爲什麼Firefox會比Chrome更嚴重地處理這個錯誤,並導致我自己的Wordpress中的javascript函數無法創建?
- 爲什麼bar ++會得到一個軟錯誤(NaN),但是「ID - 」++會得到一個硬錯誤?