简体   繁体   English

为什么使用点*和*括号访问权限来分配属性?

[英]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.

相关问题 使用以var,点和括号表示法存储的属性名称 - Use a property name stored in a var, dot and bracket notation 为什么车把使用点括号表示法来按索引访问数组? - Why does handlebars use dot bracket notation for accessing arrays by index? 实现一个函数来访问对象的属性,而不是使用点符号或方括号符号来访问属性? - Implementing a function to access an object's property versus accessing the property using dot notation, or bracket notation? 为什么点符号在我的媒体资源访问中失败? - Why dot notation failed in my property access? JavaScript属性重复[点对括号]… - JavaScript property duplicates [dot vs bracket]… 为什么我必须使用括号注释访问我的 object 属性 - Why do I have to access my object property with the bracket annotation 我可以使用方括号中的表达式通过“this”关键字访问 class 的属性吗? - Can I use expression in square bracket to access property of class by "this" keyword? 无法使用点或括号表示法来访问json / javascript对象中保存到状态后的键或数据 - Cant use dot or bracket notation to access keys or data in json/javascript object saved to state after fetch 括号中的点以访问嵌套属性 - Dots in bracket to access nested property 为什么我不能使用括号表示法+变量将属性赋值给对象? - Why can't I assign a property to an object using bracket notation + a variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM