xdhmoore xdhmoore - 15 days ago 5
Javascript Question

Javascript (0 && 1)

I'm trying to debug some JavaScript code related to Dojo localization, and I came across the following code in Dojo:

isXd = function(mid, contextRequire){
return ( 0 && 1 ) ?
contextRequire.isXdUrl(require.toUrl(mid + ".js")) :
true;
},


What is the purpose of the ternary operator here? It seems to me that
(0 && 1)
would always be
false
and that this function would always return
true
. Is this a browser compatibility thing?

Answer

The source I've found differs a bit from your code, but the explanation should fit.

The original code can be found in i18n.js which is part of the full source package:

isXd = function(mid, contextRequire){
    return (has("dojo-sync-loader") && has("dojo-v1x-i18n-Api")) ?
        contextRequire.isXdUrl(require.toUrl(mid + ".js")) :
        true;
},

The same part in i18n.js.uncompressed.js included in the release package looks like this:

isXd = function(mid, contextRequire){
    return ( 1  &&  1 ) ?
        contextRequire.isXdUrl(require.toUrl(mid + ".js")) :
        true;
},

When you take a look at the The Dojo Loader section "Options/Features", you'll see that dojo-sync-loader has a default value true.

The documentation states:

The first column is the Option/Feature as defined within the loader, the second options is whether this is a detected feature (via has.add()) or if it is just an option and its default value. With “unbuilt” source, all the features and options are available. If the loader has been built, then some of these features may have been set as staticHasFeatures and not be configurable anymore.

As dojo-sync-loader is a non-detectable feature, the likely replaced it in the built source.

Comments