Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do options["foo"] || options.foo?

Reading through the knockout.js source code I come accross this line

disposeWhenOption = options["disposeWhen"] || options.disposeWhen

I've never seen this before and everything I know about javascript says there is no case when the two sides of the || would be different. Why might have the knockout team used this construct?

like image 774
George Mauer Avatar asked Feb 17 '14 05:02

George Mauer


1 Answers

This comes as a result of symbol mangling performed by the Closure Compiler. From the 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.

Using both the dot syntax and the string literal is the "safe" way to work around this, as it helps ensure a client using an unexported part of the API but not using the closure compiler on their own code will not have any problems.

like image 175
MischaNix Avatar answered Nov 09 '22 00:11

MischaNix