简体   繁体   English

jQueryJavaScript错误-丢失; 声明前

[英]jQueryJavaScript error - missing ; before statement

I was using this statement in my code to cache the jquery selector and this was causing an error in the console. 我在代码中使用此语句来缓存jquery选择器,这在控制台中导致错误。 The error was "Missing ; before statement" 错误是"Missing ; before statement"

var $medium-image-holder = $('#image_'+itemID_value);

where itemID_value is a numeric value. 其中itemID_value是数字值。 This statement is inside a for loop 该语句在for循环内

Out of curiosity and after trying various tricks to overcome this thing, I replaced the hyphen with underscore in the variable name. 出于好奇,并尝试了各种技巧来解决此问题,然后在变量名称中用下划线替换了连字符。

var $medium_image_holder = $('#image_'+itemID_value);

Surprisingly this worked. 出乎意料的是,这种方法有效。

I want to know whether using hyphens in JavaScript variable names is not permitted. 我想知道是否不允许在JavaScript变量名称中使用连字符。 At least, I didn't know about this. 至少,我对此一无所知。 Would be very helpful if someone clarifies. 如果有人澄清会非常有帮助。

事实上,连字符不是变量名不允许的。

MDC Guidelines for variable names state; MDC变量名称准则声明;

A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); JavaScript标识符必须以字母,下划线(_)或美元符号($)开头; subsequent characters can also be digits (0-9). 后续字符也可以是数字(0-9)。 Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase). 因为JavaScript区分大小写,所以字母包括字符“ A”至“ Z”(大写)和字符“ a”至“ z”(小写)。

Your statement $medium-image-holder is interpreted as $medium - image - holder ($medium minus image minus holder). 您的陈述$medium-image-holder被解释为$medium - image - holder ($ medium减去image减去holder)。

After interpreting this statement as an algebraic expression, you try to set the outcome of it to a value with another statement ( =$('#image_'+itemID_value) ), which is not allowed. 在将此语句解释为代数表达式后,尝试使用另一条语句( =$('#image_'+itemID_value) )将其结果设置为一个值,这是不允许的。 That is where your error message comes from. 这就是您的错误消息的来源。

java script does not allow hyphen in variable name declaration variable name can only include alphabets numbers or underscore moreover variable name should only start with alphabet variable names cannot start with numbers Java脚本不允许在变量名称声明中使用连字符变量名称只能包含字母数字或下划线,而且变量名称应仅以字母变量名称开头,而不能以数字开头

cheers 干杯

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

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