eslint 使用console调试

作者: _士心_ | 来源:发表于2017-02-14 14:45 被阅读1110次

    在 JavaScript,虽然console被设计为在浏览器中执行的,但避免使用console的方法被认为是一种最佳实践。这样的消息被认为是用于调试的,因此不适合输出到客户端。通常,在发布到产品之前应该剔除console的调用。

    // 不提倡使用
    console.log("Made it here.");
    console.error("That shouldn't have happened.");
    

    Rule Details

    /*eslint no-console: "error"*/
    console.log("Log a debug level message.");
    console.warn("Log a warn level message.");
    console.error("Log an error level message.");
    

    正确 代码示例:

    /*eslint no-console: "error"*/
    // custom console
    Console.log("Hello world!");
    

    如果你在使用 Node.js,然后,console
    主要用来向用户输出信息,所以不是严格用于调试目的。如果你正在做 Node.js 开发,那么你很可能不想启用此规则。
    选项{ "allow": ["warn", "error"] }的 正确 代码示例:

    // 在js文件里面加入下面一行代码,就可以调用console.warn和console.error
    /*eslint no-console: ["error", { allow: ["warn", "error"] }] */
    console.warn("Log a warn level message.");
    console.error("Log an error level message.");
    

    相关文章

      网友评论

        本文标题:eslint 使用console调试

        本文链接:https://www.haomeiwen.com/subject/tftkittx.html