简体   繁体   中英

Why use dot *and* bracket access to assign a property?

I've just found the following line in Knockout's source code:

target.subscribe = target['subscribe'] = function …

Why are they assigning the function to the same property twice? The only difference is the way they access it. As far as I know this shouldn't make a difference with the given property name ( JavaScript property access: dot notation vs. brackets? ).

It's possible that this is done to prevent things breaking when code is minified.

target.subscribe can be minified to something like target.a , however there may be code that relies on target.subscribe still being there. For instance, you might have:

var x = 'subscribe';
target[x](something);

Assigning to both will allow the minifier to do its work, without breaking support for expression access.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM