预处理Emulator for JavaScript(定时/调试示例)

Pre-processing Emulator for JavaScript ( Timing / Debug Example )

本文关键字:调试 定时 Emulator for JavaScript 预处理      更新时间:2023-09-26

在C++中,您可以通过使用预处理指令来省略编译调试代码,以保持编译代码的快速性,而不会受到生产中不需要的调试代码的阻碍。

在JavaScript中有相关的方法吗?我过去一直在做的是注释调试代码,但我想要一种更干净的方式来做

下面的示例显示了如果debug设置为true则激活的4个if语句。然而,在生产中,当我知道它将被设置为false时,我不希望检查4次。正如我提到的,我可以把它塞进一行,然后评论出来。。。但我想要一个干净的方式?

/**
 **  cType
 */
function cType( o_p ) {
    if( debug ) {
        var t1, t2, t3, t4, i1, i2, i3; t1 = new Date().getTime();
    }
    o_p = MType[ o_p.model ].pre( o_p ); 
    if ( o_p.result !== 'complete' ) {
        if( debug ) {
            t2 = new Date().getTime();
            console.log( '---------------Send to Server - object_pipe: 'n ' + o_p.toSource() ); 
        } 
        var string_pipe = JSON.stringify( o_p );
        cMachine( 'pipe=' + string_pipe , function( string_pipe ) {
            if( debug ) { 
                console.log( '---------------Receive from Server - object_pipe: 'n ' + string_pipe ); 
                t3 = new Date().getTime();
            }
            MType[ o_p.model ].post( JSON.parse( string_pipe ) );
            if( debug ) {
                t4 = new Date().getTime(); i1 = t2-t1 ; i2 = t3-t2 ; i3 = t4-t3;
                console.log( '---------------Pre, Transit, Post = ', i1, i2, i3 );  
            }
        } );
    }
}

您总是可以通过c预处理器来传递它,比如:

gcc -E input.js -o output.js

这将允许您使用#if,甚至包括和宏。

如果您使用RequireJS,您可以使用build pragmas甚至has.js集成来在优化(缩小)时禁用/启用代码片段。

否,Javascript不是编译的,而是解释的。因此,除非您通过另一个实用程序传递非标准的Javascript(可能不再是Javascript)代码,否则不可能有预处理指令。