2016-12-30 65 views
3

我正在閱讀有關Javascript對象(http://javascriptissexy.com/javascript-objects-in-detail/)的文章,因此我將以下代碼複製粘貼到我的記事本++中,並在Chrome(版本55.0)中運行。 2883.87米)。在打開控制檯時,控制檯報告SyntaxError。 有人有一個想法,爲什麼?一切似乎都好。Javascript - SyntaxError:無效或意外的標記 - 創建對象時 - 不可見字符

// We have been using dot notation so far in the examples above, here is another example again:​ 
​var book = {title: "Ways to Go", pages: 280, bookMark1:"Page 20"}; 
​ 
​// To access the properties of the book object with dot notation, you do this:​ 
console.log(book.title); // Ways to Go​ 
console.log(book.pages); // 280 
+0

你跑哪裏了?在瀏覽器(哪一個)中,在nodejs中,完全是其他的東西? – UnholySheep

+1

當我將代碼複製並粘貼到控制檯時,我看到了2個不可見字符 - 這就是原因 –

+1

複製/粘貼可以拾取會導致這些錯誤的隱形字符。只需手動輸入代碼。 – Pointy

回答

6

如果複製的一切您只需粘貼,並在控制檯中寫,你會看到有一些Unicode字符(\u200b)在你的代碼實際上是在你得到錯誤的Invalid or unexpected token,你看不出來,因爲它們是零寬間隔,所以才刪除它們,如下圖所示

// We have been using dot notation so far in the examples above, here is another example again: 
 
var book = {title: "Ways to Go", pages: 280, bookMark1:"Page 20"}; 
 

 
// To access the properties of the book object with dot notation, you do this: 
 
console.log(book.title); // Ways to Go 
 
console.log(book.pages); // 280

你可以找到的代碼將完美運行更多關於這裏的零寬度空格字符:http://www.fileformat.info/info/unicode/char/200b/index.htm

相關問題