admin管理员组文章数量:1307007
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
3 Answers
Reset to default 4The 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:
- Non-built code has "kitchen sink" with dynamically-evaluated has() calls.
- You configure build profile with options indicating what you do/don't want in your custom build.
- 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).
- 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.
本文标签: javascriptWhy the need for if(0) and if(1)Stack Overflow
版权声明:本文标题:javascript - Why the need for if(0) and if(1) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741832286a2400003.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论