7

I have checked that π is valid for a JavaScript variable name. Also, when I try writing var π = Math.PI in the Chrome console, all is fine.

However, when I include the line var π = Math.PI inside a .js file written with Sublime Text 2 on a Mac, Chrome complains:

Uncaught SyntaxError: Unexpected token ILLEGAL

I have tried the encodings UTF-8, UTF-16 LE and UTF-16 BE, but none work. When I execute the line console.log('π'), written inside Sublime Text 2 with UTF-8, Chrome prints:

Ï€

How can I use the JavaScript variable name π in Sublime Text 2, so that Chrome understands it?

4

1 回答 1

6

检查以确保您<meta charset="utf-8">在 html 的头部设置了 a ,或者如果字符集不同,您可以在脚本标签上设置 charset 属性:

<script src="script.js" charset="utf-8">
于 2012-06-04T20:42:00.803 回答