javascript 语法错误:缺少变量名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18246015/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:11:43  来源:igfitidea点击:

SyntaxError: missing variable name

javascriptjslint

提问by Danny Beckett

I'm running JavaScript Linton a project to check for common programming errors. I'm hitting this error:

我正在一个项目上运行JavaScript Lint来检查常见的编程错误。我遇到了这个错误:

SyntaxError:missing variable name

语法错误:缺少变量名

On this line:

在这一行:

var char, font;

From Googling, I've found that that error is shown when a reserved word is used as a variable name; but judging by MDN's list, neither charnor fontis reserved.

从谷歌搜索中,我发现当保留字用作变量名时会显示该错误;但从MDN 的列表来看,既不保留char也不font保留。

What is the problem here?

这里有什么问题?

回答by Danny Beckett

Never mind, I found the answer by reading What is the 'char' keyword used for?.

没关系,我通过阅读什么是'char'关键字用于?.

Apparently charwas reserved in ECMA 3, but removed as a reserved keyword in ECMA 5.

显然char在 ECMA 3 中保留,但在 ECMA 5 中作为保留关键字删除。

I've renamed my varnow, to prevent any potential issues arising with old implementations.

我已经重命名了我的var现在,以防止旧实现出现任何潜在问题。

回答by Andrew

According to http://www.quackit.com/javascript/javascript_reserved_words.cfm

根据http://www.quackit.com/javascript/javascript_reserved_words.cfm

charis a keyword reserved by JavaScript.

char是 JavaScript 保留的关键字。

I think fontis fine.

我觉得font还好。

回答by user66001

As this question gets returned on the first page of a google search for JS Missing variable name, and I feel it is the most appropriate out of the other SO questions which mention this error, am going to detail another reason for this error here:

由于此问题在 google 搜索的第一页上返回JS Missing variable name,并且我觉得它是提及此错误的其他 SO 问题中最合适的,因此我将在此处详细说明此错误的另一个原因:

When using bookmarklet code that has %20HTMLEntity's in place of spaces, in (for example) Firefox's Scratchpad.

在(例如)Firefox 的 Scratchpad 中使用具有%20HTMLEntity 代替空格的书签代码时。

回答by ratskin

I solved this by making these changes to my code.

我通过对我的代码进行这些更改来解决这个问题。

I had:

我有:

var a = 1,
    b = 2;

I changed it to this (by mistake), which cause the error:

我把它改成了这个(错误地),这导致了错误:

var a = 1,
var b = 2;

I resolved it with this:

我用这个解决了它:

var a = 1;
var b = 2;