Javascript syntax error unexpected token illegal

青春壹個敷衍的年華 提交于 2019-12-13 04:01:37

问题


function queue_instructions(){
        var input_message = "Commands
    w?  Shows whos on the waitlist
    w+  Adds yourself to the waitlist
    w-  Removes yourself from the waitlist
    w++  Moves yourself down one spot on the waitlist
    -mods  Shows a list of available moderators
    -plays  Shows how many songs each DJ has played
    -promote  Requests a vote from everyone for you to be moved to 1st on the list
    -pull [#]  Requests a vote from everyone to pull that DJ off the booth  Number of DJ is     what number spot he is on the booth  Numbers read from left to right
    -remove [#]  Removes that number DJ from the waitlist  Must be a moderator
    -votekick [username]  Requests a vote from everyone to kick the user
     Type -help [command] for more info on a command (ie. -help w?)";
        deliver_chat(input_message);

I get a syntax error unexpected toke illegal on my Javascript console on Google chrome. Any ideas?


回答1:


You have to close those quotes on each line, and concatenate with + to the next line:

var input_message = "Commands " + 
    "w?  Shows whos on the waitlist " + 
    "w+  Adds yourself to the waitlist " + 

and so on

Did you want line breaks to be inserted with each line? If so, you can use \n:

var input_message = "Commands\n" + 
    "w?  Shows whos on the waitlist\n" + 
    "w+  Adds yourself to the waitlist\n" + 



回答2:


When you split a string over multiple lines, you need to concatenate it with + like this

var input_message = "Commands"+
"w?  Shows whos on the waitlist"+
"w+  Adds yourself to the waitlist"+
"w-  Removes yourself from the waitlist"+
"w++  Moves yourself down one spot on the waitlist"+
"-mods  Shows a list of available moderators"+
"-plays  Shows how many songs each DJ has played"+;

And also in your code, I don't find the closing curly brace } of that function.

function queue_instructions()
{
     //Stuff here.
}

you need to include it.




回答3:


Your syntax is incorrect. You'll need to either put the whole string in one line OR close the quotes in each line a do a + to concat the next line with it.




回答4:


Or you do like this:

var input_message = [
    "Commands",
    "w?  Shows whos on the waitlist",
    "w+  Adds yourself to the waitlist",
    "w-  Removes yourself from the waitlist",
    "w++  Moves yourself down one spot on the waitlist",
    "-mods  Shows a list of available moderators",
    "-plays  Shows how many songs each DJ has played",
    "-promote  Requests a vote from everyone for you to be moved to 1st on the list",
    "-pull [#]  Requests a vote from everyone to pull that DJ off the booth  Number of DJ is what number spot he is on the booth  Numbers read from left to right",
    "-remove [#]  Removes that number DJ from the waitlist  Must be a moderator",
    "-votekick [username]  Requests a vote from everyone to kick the user",
    "Type -help [command] for more info on a command (ie. -help w?)"
].join("\n");



回答5:


I notice no-one has suggested escaping the line-ending:

var multiStr = "This is the first line \
    This is the second line \
    This is more...";

Note that the line endings are included in this string…

http://davidwalsh.name/multiline-javascript-strings



来源:https://stackoverflow.com/questions/8612344/javascript-syntax-error-unexpected-token-illegal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!