I am writing a custom UI builder in js. It uses an implementation of a factory to build elements. To make sure that it was obvious which functions were part of the library versus which were part of plain old javascript I used a naming convention of function _FunctionName(). However, I find it tedious to always be doing Factory._FunctionName().
Should I just remove the naming convention (function FunctionName())or stick to it?
Are there naming convention common / best practices with regards to making libraries like this?
edit:
var __PanelFactory = function () {
//"private"
var Panels = [];
//exposed
function _GetPanel(id) {
//etc
}
return {
_GetPanel: _GetPanel,
};
};
var Factory = new __PanelFactory();
Factory. //this will show certain plain javascript functions
//like toString, constructor, hasOwnProperty, isPrototypeOf, etc...
//note that even jQuery can have the previous list used with something like
$(selector).
//So to differentiate I made sure my functions start with _
Factory._GetPanel(1);
//Should I just make it easy on myself and allow
Factory.GetPanel(1);
//Or is there value in leaving the naming convention in?
There is a question already on SO that links to a good document on Javscript coding conventions (including naming):
The key bits you’ve asked about:
This is somewhat optional, as
PanelFactorywon’t collide with plain Javascript. It might collide with other libraries if you are making a public API to be consumed by third parties, though:_) before all function names.You don’t need the underscores, as people who have been working with JavaScript for a while know the default instance-scoped functions built into every object (
toStringetc):This is just a standard convention. It doesn’t help distinguish built-in functions, but as I said in the previous item, you don’t need to.
This will help you tell the difference between a namespace, an object constructor, and an instance of an object (where you would expect custom instance-scoped functions, commonly called “methods”, to exist).