javascript 错误:'控制台未定义。[no-undef] - 括号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49525310/
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
ERROR: 'console is not defined. [no-undef] - Brackets
提问by Riddick
I'm basically a beginner with JavaScript. I've installed brackets and currently playing around with variables and functions. All the outputs work fine in the console, however I keep getting this error in the editor. https://i.imgur.com/SExglwR.png
我基本上是 JavaScript 的初学者。我已经安装了括号,目前正在使用变量和函数。所有输出在控制台中都可以正常工作,但是我在编辑器中不断收到此错误。https://i.imgur.com/SExglwR.png
How do I go about fixing this?
我该如何解决这个问题?
回答by Amaury Liet
The no-undefrule looks out for undefined variable, without any initial assumption on the environment and the global variables (consolefor instance).
该no-undef规则查找未定义的变量,对环境和全局变量(console例如)没有任何初始假设。
You can specify that you are in an environment where consoleindeed exists, by adding browserand/or nodeenvs in your .eslintrc:
您可以console通过在以下文件中添加browser和/或nodeenvs来指定您处于确实存在的环境中.eslintrc:
env: {
browser: true,
node: true,
},
More info in the rule doc
规则文档中的更多信息
回答by ummahusla
I assume it's coming from no-consolerule, which disallows calls to methods of the consoleobject.
我认为它来自无控制台规则,该规则不允许调用console对象的方法。
In JavaScript that is designed to be executed in the browser, it's considered a best practice to avoid using methods on
console. Such messages are considered to be for debugging purposes and therefore not suitable to ship to the client. In general, calls usingconsoleshould be stripped before being pushed to production.
在旨在在浏览器中执行的 JavaScript 中,避免在
console. 此类消息被视为用于调试目的,因此不适合发送给客户端。通常,console应在推送到生产之前剥离using 调用。
Examples of correct code for this rule:
此规则的正确代码示例:
/*eslint no-console: "error"*/
// custom console Console.log("Hello world!");
As a solution, you can add this to your set of rules in .eslintrc
作为解决方案,您可以将其添加到您的规则集中 .eslintrc
rules: {
'no-console': 'off'
}
回答by S.S.
Since you have it as a Capitol C, I would guess that the editor thinks you're looking for a function or class. Try lowering it from Console.log()to console.log("john won...")and see if that works.
由于您将它作为 Capitol C,我猜编辑器认为您正在寻找一个函数或类。尝试将其从Console.log()to降低console.log("john won..."),看看是否有效。
回答by Nemanja Glumac
This one was driving me crazy as well. You can edit Brackets' config JSON. It will remove the error icon from the left gutter:
这个也让我发疯了。您可以编辑 Brackets 的配置 JSON。它将从左侧装订线中删除错误图标:
{
"brackets-eslint.gutterMarks": false
}
Reference: https://github.com/brackets-userland/brackets-eslint/blob/master/README.md
参考:https: //github.com/brackets-userland/brackets-eslint/blob/master/README.md
回答by Yunjun Wang
just to have comment: /global console/
只是有评论:/全局控制台/
as the first line of your .js file, and then have: // eslint-disable-line no-console
作为 .js 文件的第一行,然后有: // eslint-disable-line no-console
at the line of the console.log("");
在 console.log("");
this will make the error go away!
这将使错误消失!

