5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails + Turbolinks上のjs内でletを使うと怒られる(Identifier 'a' has already been declared)

Last updated at Posted at 2019-11-19

turbolinks × let の相性?

かなり時間を取られたのでメモ。

index.html.erb
<script>
  let a = "Hello";
</script>

このようにerbのscriptタグ内にletで変数宣言をすると、turbolinksでの一度訪れたページへの遷移の際
ChromeではUncaught SyntaxError: Identifier 'a' has already been declaredのように怒られてしまいます。

どうやらletで宣言した変数はページ遷移しても保持されたままのようで、再度同じ名前の変数を宣言しようとするために起こるようです。
varを使えば問題なく動作します。

index.html.erb
<script>
  var a = "Hello";
</script>

なぜこうなるのか、ご存知の方はコメントお待ちしております。

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?