admin管理员组文章数量:1277896
I was reading the article on Javascript Objects (/), so I copy-pasted the following code into my Notepad++ and ran it in Chrome(Version 55.0.2883.87 m). Upon opening the console, console reports SyntaxError. Does someone have an idea why? Everything seems ok.
// 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
I was reading the article on Javascript Objects (http://javascriptissexy./javascript-objects-in-detail/), so I copy-pasted the following code into my Notepad++ and ran it in Chrome(Version 55.0.2883.87 m). Upon opening the console, console reports SyntaxError. Does someone have an idea why? Everything seems ok.
// 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
Share
Improve this question
edited Dec 30, 2016 at 16:38
Uros C
asked Dec 30, 2016 at 16:31
Uros CUros C
2,0173 gold badges25 silver badges37 bronze badges
7
- You ran it where? In a browser (which one), in nodejs, in something else entirely? – UnholySheep Commented Dec 30, 2016 at 16:32
- 1 When I copied & paste your code to the console, I saw 2 invisible characters - That's the reason – Alon Eitan Commented Dec 30, 2016 at 16:34
- 1 Copy/paste can pick up invisible characters that will cause those errors. Just type the code in by hand. – Pointy Commented Dec 30, 2016 at 16:34
-
var book = {title: "Ways to Go", pages: 280, bookMark1:"Page 20"}; console.log(book.title); console.log(book.pages);
Should work (without the invisible characters) – Alon Eitan Commented Dec 30, 2016 at 16:35 -
1
Open developer tools (Ctrl+Shift+I) and copy the code in your question in the console tab. Hit the Enter key, and then hit the
up
arrow key, and you'll see in the code red dots – Alon Eitan Commented Dec 30, 2016 at 16:37
1 Answer
Reset to default 9If you copy everything you just pasted and write it in the console, you would see that there are some unicode characters (\u200b
) in your code which are actually the Invalid or unexpected token
in the error that you are getting, you don't see them because they are zero-width spaced, so just remove them and the code would run perfectly as shown below
// 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
You can find more about the zero width space character here: http://www.fileformat.info/info/unicode/char/200b/index.htm
本文标签:
版权声明:本文标题:syntax error - Javascript - SyntaxError: Invalid or unexpected token - while creating object - Invisible character - Stack Overf 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741260801a2367583.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论