ExtJS 中的自定义类属性
在 ExtJS 中命名自定义属性的最佳实践是什么? 在名称前面加下划线是个好主意吗?
Ext.create("Ext.Window,{
height:50,
_custom:"xxx",
_action:"yyyy"
});
另一个想法是使用 data_ 前缀来模仿 html5 自定义属性约定。
What is best practice for naming custom property in ExtJS?
Is it a good idea to precede name with an underline?
Ext.create("Ext.Window,{
height:50,
_custom:"xxx",
_action:"yyyy"
});
another idea is using data_ prefix to mimic html5 custom attribute convention.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我个人不喜欢变量名中带有额外语法信息的任何内容(Uncle Bob 在 http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882),更不用说它生成的丑陋代码(例如
mywindow._custom)。
我只会给你的变量提供描述性名称,然后你的代码会读得更好,并且你不必担心与 Ext 属性的冲突(如果你担心的话)。
我更喜欢这个:
I personally don't like anything in a variable name that carries additional syntactic information (Uncle Bob dedicates a whole section to this principle in http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882), not to mention the ugly code it produces (e.g.
mywindow._custom
).I would just give your variables descriptive names, and then your code will read better and you shouldn't have to worry about collision with Ext properties (if you were worried about).
I like this much better:
同意其他人的观点,并补充说,在大多数语法中,下划线或多或少是私有变量的公认标准(尽管 Ext JS 本身不使用它,并通过注释将私有变量标记为私有或按惯例未记录)。如果您坚持选择某种约定,我肯定会选择其他方式来命名公共配置,尽管我同意其他人的观点,这可能没有必要。我猜想在大多数实际情况下,您的自定义属性不会太通用而导致冲突(如果是的话,您可能为要添加的内容选择了糟糕的名称)。
Agreed with others, and will add that the underscore in particular is more or less an accepted standard for private variables in most syntaxes (though Ext JS itself does not use it, and keeps private variables marked as private via comments or undocumented by convention). I definitely would choose some other way to name public configs if you insist on choosing some convention, though I agree with others that it's probably not necessary. I would guess that in most real cases, your custom properties are not going to be so generic as to clash (and if they are, you probably chose poor names for whatever you're adding).
您必须记住,您正在设计一个可能被其他人使用的自定义属性。这意味着它必须描述其行为。这才是最重要的。使用 _ 的情况非常罕见,并且对于使用代码的开发人员来说并不自然,因此它必须是用户友好的。
You have to remember that you're designing a custom property that might be used by others. This means that it has to be descriptive of its behavior. That's what is most important. Using an _ is very rare and isn't really natural to developers using the code, so it's gotta be user friendly.
我决定使用
_
作为自定义变量的前缀。因为:如果 Sencha 使用一种机制来防止混淆,那就更好了。
I decided to to use
_
as a prefix for custom variables. because:It would be better if Sencha used a mechanism to prevent the mixing up.