简体   繁体   English

Javascript:“未捕获到的SyntaxError:意外令牌{”

[英]Javascript: “Uncaught SyntaxError: Unexpected token {”

I'm writing my first javascript code and when I try to run it, I get the following error 我正在写我的第一个javascript代码,当我尝试运行它时,出现以下错误

"Uncaught SyntaxError: Unexpected token {" “未捕获的SyntaxError:意外令牌{”

Why is this happening in this case? 为什么在这种情况下会发生这种情况?

var red = [0, 100, 63];
var orange = [40, 100, 60];
var green = [75, 100, 40];
var blue = [196, 77, 55];
var purple = [280, 50, 60];

var myName = "Manuel";
letterColors = [red, orange, green]
If (15 > 5 ){
bubbleShape = "circle"
}
else{
    bubbleShape = "square"
}

drawName(myName, letterColors);

If should be if . If应该的if Remember, Javascript is case-sensitive, and all JS keywords are lower-case. 请记住,Javascript区分大小写,所有JS关键字均为小写。

As an aside... 作为旁白...

You are missing a semicolon on several lines, but, as Brad pointed out in the comments, Javascript does not always require semicolons at the end of statements, unless you need to delimit two adjacent statments. 您在多行上缺少分号,但是,正如Brad在注释中指出的那样,除非您需要分隔两个相邻的语句,否则Javascript并不一定总是在语句末尾使用分号。 In this particular case, the absense of these semicolons isn't causing any errors. 在这种特殊情况下,这些分号的缺失不会引起任何错误。 That said, it's best to get in the habit of including even non-essential semicolons at the end of your statements to avoid accidentally introducing syntax errors later. 就是说,最好养成在语句末尾甚至包括非必要分号的习惯,以避免以后意外引入语法错误。

If (15 > 5 ){ //<<< should be if as javascript is case-sensitive
bubbleShape = "circle"
}
else{
    bubbleShape = "square"
}

Working: 工作:

var red = [0, 100, 63];
var orange = [40, 100, 60];
var green = [75, 100, 40];
var blue = [196, 77, 55];
var purple = [280, 50, 60];

var myName = "Manuel";
letterColors = [red, orange, green];
if (15 > 5 ){
bubbleShape = "circle";
}
else{
    bubbleShape = "square";
}

drawName(myName, letterColors);

I wouldn't set bubbleShape as a global but pass it as a variable to the function like you do it with your other vars. 我不会将bubbleShape设置为全局变量,而是将其作为变量传递给函数,就像使用其他变量一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM