简体   繁体   English

JavaScript 中的“如果调试”?

[英]"if debug" in JavaScript?

Is there anything in JavaScript or Visual Studio to detect if the code is used in debug-mode? JavaScript 或 Visual Studio 中是否有任何内容可以检测代码是否在调试模式下使用? Something like "#if DEBUG" in C#, but for JavaScript?类似于 C# 中的“#if DEBUG”,但对于 JavaScript?

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.exe command-line tool for find and replace in files. 在这里,我发现了fnr.exe命令行工具,用于在文件中查找和替换。 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.exe file into it, and created a batch file like this. 所以此时我在项目目录中创建了一个名为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: 并在Release配置中配对:

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中为Debug和for Release配置提供日志记录,跟踪,特殊逻辑。

No. 没有。

#if / #endif are 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是一种未预编译的脚本语言,因此拥有这样的预处理程序指令没有多大意义。

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 这里好文章

Late in the party, but here is what I do on Visual Studio晚会,但这是我在 Visual Studio 上所做的

// #region Uncomment when in debug mode
// localStorage.clear();
// #endregion

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

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