Javascript 未捕获的语法错误:意外的令牌变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4560951/
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
Uncaught SyntaxError: Unexpected token var
提问by Bob R
I have an error Uncaught SyntaxError: Unexpected token var displayed between (1) and (2) its a very odd error and it doesn't make sense at all.
我有一个错误 Uncaught SyntaxError: Unexpected token var display between (1) and (2) 这是一个非常奇怪的错误,它根本没有意义。
if ($hiddenimage.length==0) { //if this is the first time moving over or clicking on the anchor link
var $hiddenimage=$('<img src="'+this.href+'" />').appendTo($hiddenimagediv) //populate hidden div with enlarged image
$hiddenimage.bind('loadevt', function(e){ //when enlarged image has fully loaded
loadarea.empty().append($.thumbnailviewer2.buildimage($, $anchor, s, options)
(1) - var $targetimage=$.thumbnailviewer2.buildimage($, $anchor, s, options) //create reference actual enlarged image
(2) - $loadarea.empty().append($targetimage) //show enlarged image
$.thumbnailviewer2.showimage($targetimage, s)
})
回答by cdhowie
Count the open parentheses on this line:
计算这一行的左括号:
loadarea.empty( ).append($.thumbnailviewer2.buildimage($, $anchor, s, options)
^ ^ ^ ^ ^
1 0 1 2 1
Add another closing paren; the parser thinks you're still specifying arguments to the append()
function, and the var
keyword is invalid in this context.
添加另一个关闭括号;解析器认为您仍在为append()
函数指定参数,并且var
关键字在此上下文中无效。
Also, use semicolons. If not for your sake, do it for Douglas' health.
另外,使用分号。如果不是为了你,那就为了道格拉斯的健康。
回答by mangrove108
I had a similar error message in the console with the minifier parsing my javascript source code. I found that using // comments
like so always interrupted the minification process, and gave me an error in the console. Therefore i switchedall /* comments */
like so. MDN Javascript CommentsAnd immediately everything parsed as expected. Hope it helps.
我在控制台中有一条类似的错误消息,其中 minifier 解析了我的 javascript 源代码。我发现// comments
像这样使用总是会中断缩小过程,并在控制台中给我一个错误。因此,我像这样切换了所有内容/* comments */
。MDN Javascript 注释立即一切都按预期解析。希望能帮助到你。