[英]Why use dot *and* bracket access to assign a property?
I've just found the following line in Knockout's source code: 我刚刚在Knockout的源代码中找到以下行:
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? ). 据我所知,这与给定的属性名称( JavaScript属性访问:点符号与方括号? )没有什么区别。
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. 可以将target.subscribe
缩小为target.a
,但是可能仍然存在依赖target.subscribe
代码。 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. 分配给这两者将使Minifier能够完成其工作,而不会破坏对表达式访问的支持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.