有没有办法在 XBL 中创建私有方法?
Is there a way to create a private method in XBL?
--update
The only documentation on MDN says nothing about private methods, but it's a wiki that's not always complete..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题的答案是 XBL 不支持私有方法。然而,这并不意味着您只需公开暴露您的公共方法,并且不战而屈人之兵地接受这种情况。您可以使用一些选项来帮助传达方法是私有的或帮助阻止使用或修改它们:
在方法名称中使用下划线:
Mozilla 建议使用下划线来标记方法和字段设为私有。此外,许多 JavaScript 库在开发人员希望标记为私有的方法中使用下划线。虽然新手开发人员可以忽略这一点并仍然调用该方法,但大多数对 JavaScript 库、Firefox 扩展开发或一般 JavaScript 有一些基本经验的人应该知道当方法前面带有下划线时意味着什么。
使用继承来隐藏私有方法:
眼不见,心不烦。
在某些语言中,一系列子类共有的功能通常会转移到抽象基类中。在子类中,继承的方法不会在子类代码中看到。
尽管这绝对不是“私有”的,但您可以将“私有”方法封装在 XBL 绑定中,并将公共方法放置在扩展父绑定的 XBL 绑定中。继承是 XBL 最强大的功能之一,这有助于保护您的私有方法不被使用,因为它们不会出现在开发人员直接与之交互的 XBL 绑定中。
然后,您可以在父级中添加大量注释,描述“私有”功能的目的,并且它并不意味着公开。
请记住,即使您可以将方法标记为私有,但这仍然无法阻止有决心的人。人们仍然可以简单地将方法标记为“公共”并无论如何使用它。
以下是 XBL 的文档,它断言方法是私有的,并且还讨论了继承:
https://developer.mozilla.org/en/XUL_School/Custom_XUL_Elements_with_XBL
The answer to your question is that XBL does not support private methods. However, this doesn't mean that you simply have to leave your public methods openly exposed and just accept this situation without a fight. there are some options that you have at your disposal that can help communicate that a method is private or help discourage using or modifying them:
Use an underscore in method names:
Mozilla recommends using an underscore to mark methods and fields as private. Additionally, many JavaScript libraries use underscores in the methods that the developers wish to mark as private. Although a novice developer could ignore this and still invoke the method, most people who have some basic experience with JavaScript libraries, Firefox Extension development, or JavaScript in general should know what you mean when you have a method preceded by an underscore.
Use inheritance to hide private methods:
Out of sight, out of mind.
In some languages, functionality that is common to a series of subclasses is oftentimes moved to a base abstract class. In the subclass, the inherited methods won't be seen in the subclass code.
Although this is definitely not "private", you could encapsulate your "private" methods in an XBL binding and place your public methods in an XBL binding that extends the parent binding. Inheritance is one of the most powerful features of XBL, and this could help protect your private methods from being used simply because they won't appear in the XBL binding the developer is directly interacting with.
You could then put extensive comments in the parent that describes the purpose of the "private" functionality and that it isn't meant to be public.
Keep in mind that even if you could mark a method as private, this still won't stop someone who is determined. One could still simply mark the method as "public" and use it anyway.
Here is documentation on XBL, which asserts that methods are private, and also discusses inheritance:
https://developer.mozilla.org/en/XUL_School/Custom_XUL_Elements_with_XBL