最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why the need for if(0) and if(1) - Stack Overflow

programmeradmin0浏览0评论

Look at this piece for code in ArcGIS 3.0 for javascript. /?v=3.0

Inside there is "if(0)" and "if(1)", why is there a need for this? Isn't if(0) always false and if(1) always true?

Look at this piece for code in ArcGIS 3.0 for javascript. https://serverapi.arcgisonline./jsapi/arcgis/?v=3.0

Inside there is "if(0)" and "if(1)", why is there a need for this? Isn't if(0) always false and if(1) always true?

Share Improve this question asked Jul 6, 2012 at 2:12 yeeenyeeen 4,94512 gold badges53 silver badges74 bronze badges 2
  • 5 Maybe because it is an obfuscated code: en.wikipedia/wiki/Obfuscation_(software) – Jack Commented Jul 6, 2012 at 2:14
  • @Jack Why would Dojo obfuscate the code they offer unpressed at dojotoolkit/download? – Ian Hunter Commented Jul 6, 2012 at 2:20
Add a ment  | 

3 Answers 3

Reset to default 4

The Dojo build tools are what does that (under given build options), but not for obfuscation. If you look at non-built dojo.js and corresponding built dojo.js.unpressed.js files, you can see that the build tool is replacing has("somefeature") calls with hardwired true/false tests. As noticed, this can and does create unreachable code. Why do this? Because then a smart optimizing piler (e.g. Google Closure) can prune all that dead code out, resulting in a smaller file (sometimes MUCH smaller...that's the point).

Conceptually, it goes something like this:

  1. Non-built code has "kitchen sink" with dynamically-evaluated has() calls.
  2. You configure build profile with options indicating what you do/don't want in your custom build.
  3. Build process substitutes dynamic has() calls [for corresponding build options] with hardwired true/false tests (or better way to look at it is in/out tests).
  4. Closure piler removes the "out" code during minification.

Check out current "Dojo Build System" documentation and http://jamesthom.as/blog/2012/08/03/finding-nano/ for more info. Also, here's a good low/code-level description of this process.

P.S. "if(0)/if(1)" isn't really obfuscation...kinda the opposite. If someone wanted to confuse, they'd more likely have "if(a)...if(b)...if(c)..." with vars set far, far away. However, minifiers produce more obfuscated code than that on their own. Check out dojo.js source before and after it's been run through Closure; the end product bears little resemblance to original.

Yes, 0 is always false and 1 is always true.

However as you can see in the code, the pany considers it their trade secret:

COPYRIGHT 2009 ESRI

 TRADE SECRETS: ESRI PROPRIETARY AND CONFIDENTIAL
 Unpublished material - all rights reserved under the
 Copyright Laws of the United States and applicable international
 laws, treaties, and conventions.

It is mon to obfuscate such code (i.e. making it harder to read). One of the ways is inserting useless statements like the if(1) and if(0) you have seen.

You can read more about Obfuscation here.

Another possible explanation is that these if statements are used in place of real logic that has yet to be implemented, as @mvbl fst mentioned.

This may be used in place of real if() statement for which actual logic has not been implemented yet. And as @houbysoft mentioned, they are interpreted as boolean false and true. So for the mean time they use false or true to make sure statements inside always execute (or not) and intend to add actual checks later.

发布评论

评论列表(0)

  1. 暂无评论