问题描述
我正在js中编写自定义UI构建器。它使用工厂的实现来构建元素。为了确保哪些函数是库的一部分而哪些是普通的旧javascript的一部分,我使用了函数_FunctionName()
的命名约定。但是,我发现总是做 Factory._FunctionName()
这很乏味。
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()
.
我应该删除命名约定( function 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?
编辑:
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?
推荐答案
SO上已经有一个问题链接到关于Javscript编码约定的良好文档(包括命名):
There is a question already on SO that links to a good document on Javscript coding conventions (including naming):
- javascript naming conventions
- http://javascript.crockford.com/code.html
您询问的关键部分:
- 使用名称空间(带有初始名称)。
这在某种程度上是可选的,因为 PanelFactory
不会与普通的Javascript冲突。如果你要让第三方使用公共API,它可能会与其他库冲突,但是:
This is somewhat optional, as PanelFactory
won'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:
var MyApp = MyApp || {};
MyApp.__PanelFactory = function () { // ...
- 在所有函数名称之前删除下划线(
_
)。 - Get rid of the underscores (
_
) before all function names.
您不需要下划线,因为已经使用JavaScript一段时间的人知道内置于每个对象的默认实例范围函数( toString
等):
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 (toString
etc):
MyApp.PanelFactory = function () {
// ...
function GetPanel(id) {
//etc
}
- 名称具有初始大写字母名称的对象构造函数,但是名称为全局或名称空间作用域的函数以及具有初始小写名称的实例作用域函数:
这只是一个标准惯例。它无法区分内置函数,但正如我在上一个项目中所说,您不需要。
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.
MyApp.PanelFactory = function () {
// ...
function getPanel(id) {
//etc
}
- 用初始小写名称命名局部变量:
这将帮助您区分命名空间,对象构造函数和对象实例(您希望自定义实例范围的函数,通常称为方法)存在的区别)。
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).
var factory = new MyApp.PanelFactory();
var panel = factory.getPanel();
这篇关于我应该如何命名我的javascript库函数,以便它们可以与普通的旧javascript区分开来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!