JavaScript 中的“如果调试”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22343819/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:58:02  来源:igfitidea点击:

"if debug" in JavaScript?

javascriptdebuggingvisual-studio-debugging

提问by Martin

Is there anything in JavaScript or Visual Studio to detect if the code is used in debug-mode? Something like "#if DEBUG" in C#, but for JavaScript?

JavaScript 或 Visual Studio 中是否有任何内容可以检测代码是否在调试模式下使用?类似于 C# 中的“#if DEBUG”,但对于 JavaScript?

采纳答案by M?rten Wikstr?m

No.

不。

#if/#endifare preprocessor directives in C# (and other languages) that tells the compiler to conditionally include/exclude a section of code when compiling.

#if/#endif是 C#(和其他语言)中的预处理器指令,它告诉编译器在编译时有条件地包含/排除一段代码。

JavaScript is a script language that is not precompiled, and therefore it would not make much sense to have preprocessor directives like these.

JavaScript 是一种未预编译的脚本语言,因此拥有这样的预处理器指令没有多大意义。

回答by Zoltán Tamási

A bit late, but I needed the same and could not give up until a viable solution.

有点晚了,但我需要同样的东西,并且在找到可行的解决方案之前不能放弃。

I have a kind of "main" javascript file, where I have a line like:

我有一种“主要”javascript文件,其中有一行:

Site.DEBUG = false;

Then in the code I can check for this constant. Now I needed to solve that at build time, some automation would set this for me according to project configuration. Here I've found fnr.execommand-line tool for find and replace in files. It's a quite good piece of utility, would be worth to check out anyway. So at this point I've created a folder in the project directory called BuildScripts, I've copied the fnr.exefile into it, and created a batch file like this.

然后在代码中我可以检查这个常量。现在我需要在构建时解决这个问题,一些自动化会根据项目配置为我设置这个。在这里,我找到了用于在文件中查找和替换的fnr.exe命令行工具。这是一个相当不错的实用程序,无论如何都值得一试。所以此时我已经在项目目录中创建了一个名为 的文件夹BuildScripts,我已经将fnr.exe文件复制到其中,并创建了一个这样的批处理文件。

switch_client_debug.bat

REM Params: path to folder, filename, change-DEBUG-from-this, to-this
fnr.exe --cl --dir "%1" --fileMask "%2" --caseSensitive --showEncoding --find "DEBUG = %3" --replace "DEBUG = %4"

Then I defined the corresponding pre-build events at the web project like this:

然后我在 web 项目中定义了相应的预构建事件,如下所示:

cd $(ProjectDir)BuildScripts
call switch_client_debug.bat $(ProjectDir)ts site.ts false true

and its pair at Release config:

及其在发布配置中的配对:

cd $(ProjectDir)BuildScripts
call switch_client_debug.bat $(ProjectDir)ts site.ts true false

Now everything works like a charm and I can have logging, tracing, special logic for Debug and for Release configuration in Javascript.

现在一切都像魅力一样工作,我可以在 Javascript 中为调试和发布配置进行日志记录、跟踪、特殊逻辑。

回答by alessandro

Only for IE there is the conditional compilation:

只有 IE 才有条件编译:

/*@cc_on
@set @version = @_jscript_version
@if (@_win32)
document.write("You are running 32 bit IE " + @version);
@elif (@win_16)
document.write("You are running 16 bit IE " + @version);
@else @*/
document.write("You are running another browser or an old IE.");
/*@end @*/

nice article here

好文章在这里