How to check if 'debugger;' keyword exists?

后端 未结 4 1960
野趣味
野趣味 2021-02-08 11:06

Sometimes some developers forgot to remove debugger; in javascript code, and it produce javascript error on IE. How can you check (like for the console: if(wi

4条回答
  •  借酒劲吻你
    2021-02-08 11:43

    You could attempt to compile a function that declares debugger as a local variable. If debugger is reserved as a keyword, the JS engine will throw an error which you can catch.

    var debuggerIsKeyword = false;
    try {
        new Function("var debugger;");
    } catch(e) {
        debuggerIsKeyword = true;
    }
    

    However I'm not sure that knowing whether a keyword exists or not is actually helpful.

提交回复
热议问题