Is there a way to create开发者_如何学JAVA 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..
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
精彩评论