Update: I found an alternate method to pick up the Visual Studio build target settings and pull in a selected JavaScript file. See http://www.kraigbrockschmidt.com/differentiate-debug-release-builds-javascript/

I answered a question recently on StackOverflow about conditional compilation in JavaScript. The question specifically asked about writing code that could target multiple platforms, especially the need on Windows to wrap some code with execUnsafeLocalFunction.

Of course, JavaScript does not have a notion of #ifdef directives and such that are normally used for this purpose in languages like C++. It's necessary, then, to write your own stub functions that wrap others like execUnsafeLocalFunction. In that function you'd test whether the function exists by checking the namespaces. Something like:

function callUnsafeFunction(func,) {
    if (MSApp && MSApp.execUnsafeLocalFunction) {
        return MSApp.execUnsafeLocalFunction(func);
    } else {
        return func()
    }
}

Alternately, you can check if the function exists in the namespace, and if not, add you own function of the same name as a passthrough, something like this (I haven't tested such code):

if (!(MSApp && MSApp.execUnsafeLocalFunction)) {
    window.MSApp = {
        execUnsafeLocalFunction: function(func) {
            func();
        }
    };
}

In both cases we're using a conditional check for the existence of a namespace/function to the same effect as #ifdef directives.


Comments are closed