简体   繁体   English

在JavaScript中“抛出”警告?

[英]“throw” a warning in JavaScript?

In FireFox, there is this cute little Error-Console [Ctrl+Shift+J] where I can add an error by throwing it from JavaScript. 在FireFox中,有一个可爱的小错误控制台[Ctrl + Shift + J],我可以通过从JavaScript中抛出它来添加错误。 Is there a way to display a warning or a message, too? 有没有办法显示警告或消息? I dont mean console.warn() , i really want it in the error-console, i'd just prefer to have it a warning mark instead of an error marking. 我不是指console.warn() ,我真的想在错误控制台中使用它,我只是希望它有一个警告标记而不是错误标记。

Is there a way to accomplish this? 有没有办法实现这个目标?

There is no such thing as a Warning in JavaScript. 在JavaScript中没有警告这样的东西。 All errors are fatal. 所有错误都是致命的。

console.warn will, in browsers that implement it, print a warning-level message in the console (similar to malformed HTML or security warnings). console.warn将在实现它的浏览器中,在console.warn中打印警告级消息(类似于格式错误的HTML或安全警告)。

Personally, I'd just write console.log("Warning: It's late and I'm drunk."); 就个人而言,我只是写console.log("Warning: It's late and I'm drunk.");

JavaScript has a number of built-in error constructors, such as Error() , SyntaxError() , and TypeError() , and others, which are used with the throw statement. JavaScript有许多内置的错误构造函数,例如Error()SyntaxError()TypeError() ,以及与throw语句一起使用的其他构造函数。 These objects contain interesting information about the error such as the name property of the constructor function that created the object, or the message that was passed in to the object. 这些对象包含有关错误的有趣信息,例如创建对象的构造函数的name属性,或传递给对象的消息。

The fun part about JavaScript is that you can throw any objects you want. 关于JavaScript的有趣部分是你可以抛出你想要的任何对象。 So based on this concept, you can do something like throw {name: "some name", message: "some message", remedy: callback} , catch this error in a top level try/catch statement and output the contents using console.log . 因此,基于这个概念,您可以执行诸如throw {name: "some name", message: "some message", remedy: callback} ,在顶级try/catch语句中捕获此错误并使用console.log输出内容console.log

Hope this helps. 希望这可以帮助。

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

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