Javascript 运行 www.jshint.com 时未定义“警报”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8173364/
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
"alert" is not defined when running www.jshint.com
提问by Adam Zalcman
I fixed this by simply adding var alert;
However, is this what I should be doing to get the pesky error message to go away? Here is the fix. Here is the fail on www.jshint.com.
我通过简单地添加解决了这个问题,var alert;
但是,这是我应该做的才能让讨厌的错误消息消失吗?这是修复。这是www.jshint.com上的失败。
I'm trying to learn from the error it throws..not necessarily make them go away.
我试图从它抛出的错误中吸取教训......不一定会让它们消失。
(function () {
"use strict";
var alert; // added this in to fix
function initialize_page()
{
alert ("hi");
}
addEventListener('load', initialize_page);
})();
回答by Boris Brdari?
Instead of
代替
alert('message')
you should use
你应该使用
window.alert('message');
Because this method is defined in window
object.
因为这个方法是在window
object 中定义的。
This of course assumes you have browser
option set to true
in your .jshintrc
, so this way jshint will know window object is exposed.
这当然假设你在你的 中browser
设置了选项,这样 jshint 就会知道 window 对象是公开的。true
.jshintrc
"browser" : true, // Standard browser globals e.g. window, document.
*The same thing happens with confirm()
.
*同样的事情发生在confirm()
.
回答by Adam Zalcman
Thisdocumentation says the following about the browser
option:
本文档说明了有关该browser
选项的以下内容:
This option defines globals exposed by modern browsers: all the way from good ol' document and navigator to the HTML5 FileReader and other new developments in the browser world. Note: this option doesn't expose variables like alert or console. See option devel for more information.
这个选项定义了现代浏览器公开的全局变量:从优秀的文档和导航器到 HTML5 FileReader 和浏览器世界中的其他新发展。注意:此选项不会公开警报或控制台等变量。有关更多信息,请参阅选项开发。
and the following about the devel
option:
以及关于该devel
选项的以下内容:
This option defines globals that are usually used for logging poor-man's debugging: console, alert, etc. It is usually a good idea to not to ship them in production because, for example, console.log breaks in legacy versions of Internet Explorer.
此选项定义了通常用于记录穷人调试的全局变量:控制台、警报等。通常最好不要在生产中发布它们,因为例如,console.log 在旧版 Internet Explorer 中会中断。
You have browserenabled and develdisabled. You can control these with checkboxes under "Assume" on the jshint original page. I also recommend heeding the warning in the documentation ;-)
您启用了浏览器并禁用了开发。您可以使用 jshint 原始页面上“假设”下的复选框来控制这些。我还建议注意文档中的警告;-)
回答by Joshua
Set "devel:true" in the Options. This enables things like Alert, console, etc.
在选项中设置“devel:true”。这可以启用警报、控制台等。
See documentation here: http://jshint.com/docs/options/
请参阅此处的文档:http: //jshint.com/docs/options/
回答by Aliaksandr Shpak
Use .jshintrc
file or CTRL + ,in VS Code, to edit options for jshint.
使用.jshintrc
file 或CTRL + ,在VS Code中编辑jshint 的选项。
in js alert(data.status); or window.alert(data.status);
在 js 警报(data.status)中;或 window.alert(data.status);
"window": true,
"alert": true
or best
或最好
"devel": true,
{
"esversion": 6,
"browser": true,
"undef": true,
"varstmt": true,
"forin": true,
"unused": true,
"funcscope": true,
"lastsemic": true,
"moz": true,
"jquery": true,
"module": true,
"devel": true,
"globals": {
"window": true,
"document": true,
"console": true,
"alert": true
}
}
}
回答by Bhargavi Gopalachar
function prod(arr) {
let largest = 0;
let secondLargest = 0;
const len = arr.length;
for (let i = 0; i < len; i++) {
if (arr[i] > largest) {
secondLargest = largest;
largest = arr[i];
}
else if (arr[i] < largest && arr[i] > second_largest) {
secondLargest = arr[i]
}
}
return largest * secondLargest;
}
console.log(prod([2, 4, 7, 8]));
回答by Drew Hawken
Instead of:
代替:
alert('message')
I use:
我用:
var alert = window['alert'];
回答by DrStrangeLove
try passing window object in :
尝试传递窗口对象:
(function (global) {
"use strict";
var alert; // added this in to fix
function initialize_page()
{
global.alert ("hi");
}
addEventListener('load', initialize_page);
})(window);
回答by designbykhalid
declare alert as variable and it will work without any settings:
将警报声明为变量,它无需任何设置即可工作:
for example:
例如:
var alert;
alert('hello world');