阅读我遇到的 knockout.js 源代码 this line
disposeWhenOption = options["disposeWhen"] || options.disposeWhen
我以前从未见过这种情况,而且我对 javascript 的所有了解都表明 ||
的两侧不会不同。为什么 knockout 队伍可能会使用这种结构?
最佳答案
这是由 Closure Compiler 执行的符号重整的结果。来自Closure Compiler docs :
Closure Compiler compilation never changes string literals in your code, no matter what compilation level you use. This means that compilation with ADVANCED_OPTIMIZATIONS treats properties differently depending on whether your code accesses them with a string. If you mix string references to a property with dot-syntax references, Closure Compiler renames some of the references to that property but not others. As a result, your code will probably not run correctly.
同时使用点语法和字符串文字是解决此问题的“安全”方法,因为它有助于确保使用 API 的未导出部分但不在自己的代码上使用闭包编译器的客户端不会有任何问题。
关于javascript - 为什么选项 ["foo"] ||选项.foo?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21821493/