SyntaxError:Chrome 的 Javascript 控制台中的意外标识符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5000114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SyntaxError: Unexpected Identifier in Chrome's Javascript console
提问by mjmitche
I tested this javascript in Chrome's Javascript console and it returned SyntaxError: Unexpected Identifier
.
我在 Chrome 的 Javascript 控制台中测试了这个 javascript,它返回SyntaxError: Unexpected Identifier
.
I got this code from a tutorial and was just testing Chrome's console so i expected it to work, unless I'm using the console wrong?
我从教程中得到了这段代码,只是在测试 Chrome 的控制台,所以我希望它可以工作,除非我使用的控制台错误?
Code:
代码:
var visitorName = "Chuck";
var myOldString = "Hello username. I hope you enjoy your stay username.";
var myNewString = myOldString.replace ("username," visitorName);
document.write("Old String = " + myOldString);
document.write("<br/>New string = " + myNewString);
Output:
输出:
SyntaxError: Unexpected identifier
回答by David Tang
The comma got eaten by the quotes!
逗号被引号吃掉了!
This part:
这部分:
("username," visitorName);
Should be this:
应该是这样的:
("username", visitorName);
Aside: For pasting code into the console, you can paste them in one line at a time to help you pinpoint where things went wrong ;-)
旁白:要将代码粘贴到控制台中,您可以一次将它们粘贴在一行中,以帮助您查明问题出在哪里;-)
回答by Robin
Replace
代替
var myNewString = myOldString.replace ("username," visitorName);
with
和
var myNewString = myOldString.replace("username", visitorName);
回答by M. Habib
I got this error Unexpected identifier
because of a missing semi-colon ;
at the end of a line. Anyone wandering here for other than above-mentioned solutions, This might also be the cause of this error.
Unexpected identifier
由于;
行尾缺少分号,我收到此错误。除了上述解决方案之外,任何人都在这里徘徊,这也可能是导致此错误的原因。
回答by ashish.chotalia
Write it as below
写成下面这样
<script language="javascript">
var visitorName = 'Chuck';
var myOldString = 'Hello username. I hope you enjoy your stay username.';
var myNewString = myOldString.replace('username', visitorName);
var myNewString = myOldString.replace('username',visitorName);
document.write('Old String = ' + myOldString);
document.write('<br/>New string = ' + myNewString);
</script>