javascript javascript中的宏定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5980708/
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
macro definition in javascript
提问by Kiran
Is there a way that I can define a macro similar to C/C++ macros in Javascript?
有没有办法在 Javascript 中定义类似于 C/C++ 宏的宏?
I want to use this for debug statements: Something like
我想将其用于调试语句:类似
#ifdef TEST__
#define MYDEBUG(##x) debug(__FILE__,x)
#else
#define debug
#endif
Not necessarily similar, but I want to acheieve that functionality. Is there a way I can do that?
不一定相似,但我想实现该功能。有没有办法做到这一点?
Thanks
谢谢
回答by JeanHuguesRobert
var de = false; // true when debugging
function bug( msg ){ ... display msg ... }
Usage:
用法:
de&&bug( "hello world")
When "de" is false (production mode), the overhead is minimal.
当“de”为假(生产模式)时,开销最小。
回答by Olly
There isn't a way to do this in JavaScript. You could have a global variable like
在 JavaScript 中没有办法做到这一点。你可以有一个全局变量,比如
var isDebugging = false;
Then when writing code, just check if the variable is true. Obviously this will create some unwanted overhead with file size, and a very slight performance loss. But other than specifying your own format, and running the code though a tool to strip debugging code out before you upload.
然后在编写代码时,只需检查变量是否为真。显然,这会在文件大小方面产生一些不必要的开销,并且会造成非常轻微的性能损失。但除了指定您自己的格式,并通过一个工具运行代码以在上传之前去除调试代码。
Something like
就像是
var foo = function() {
<!-- document.write( "blah" ); -->
};
For a release build, you would remove everything inside the tags, inclusive. And for a debug build you could just remove the tags, but keep the code. Something like this could be performed with an Ant build script or similar.
对于发布版本,您将删除标签内的所有内容,包括在内。对于调试版本,您可以删除标签,但保留代码。可以使用 Ant 构建脚本或类似脚本执行类似的操作。
回答by aMarCruz
For those who are still interested:
对于那些仍然感兴趣的人:
https://github.com/dcodeIO/Preprocessor.js
https://github.com/dcodeIO/Preprocessor.js
A JavaScript source file preprocessor in pure JavaScript, e.g. to build different versions of a library.
纯 JavaScript 中的 JavaScript 源文件预处理器,例如用于构建不同版本的库。
Examples:
例子:
// #ifdef FULL
console.log("Including extension");
// #include "path/to/extension.js"
// #else
console.log("Not including extension");
// #endif
// #if 1==2
console.log("1==2");
// #elif 2==2
console.log("2==2");
// #endif
回答by mym
With Builder– https://github.com/electricimp/Builder, you can do like:
使用Builder– https://github.com/electricimp/Builder,您可以执行以下操作:
@macro MYDEBUG(x)
debug(@{__FILE__}, @{x});
@end
...
@{MYDEBUG(100500)}
Also supports for includes directly from GitHub.
还支持直接来自 GitHub 的包含。
回答by sapht
Javascript has no macros since there is no compiler. You could use console.log
and write a regex to strip those statements when deploying.
Javascript 没有宏,因为没有编译器。您可以console.log
在部署时使用并编写一个正则表达式来去除这些语句。
回答by stivlo
While is true that there is no compile time as @sapht said, you can pre-process your files if you want. Typically I use an ant script to combine many Javascript files together and add build information.
正如@sapht 所说,确实没有编译时间,但您可以根据需要对文件进行预处理。通常,我使用 ant 脚本将许多 Javascript 文件组合在一起并添加构建信息。
From a google search I see there is a Javascript preprocessor that you may find interesting: http://www.bramstein.com/projects/preprocess/
从谷歌搜索我看到有一个 Javascript 预处理器,你可能会觉得有趣:http: //www.bramstein.com/projects/preprocess/