English 中文(简体)
我该如何为我的书写图书馆功能命名, 使它们与普通的旧书写功能不同?
原标题:How should I name my javascript library functions so they are differentiable from plain old javascript?
  • 时间:2012-05-21 23:59:36
  •  标签:
  • javascript

我在js 中写下自定义 UI 构建器。 它使用工厂的安装来构建元素。 要确定什么功能是图书馆的一部分, 与什么功能是普通旧的 javascript I 的一部分, 我使用了一个命名公约 < code> 函数_ functionName () 。 然而, 我发现总是在做 < code> Factory._ FunctionName () 。

我是否应该删除命名公约 ( conference 函数Name () ) 或坚持它?

在使图书馆成这样方面是否有命名公约的共同/最佳做法?

编辑 :

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?
最佳回答

在战略目标上已经存在一个问题,它与关于Javrence编码公约(包括命名)的良好文件有关:

您询问的关键位数 :

  • Use namespaces (with inital-caps names).

这有点可选性,因为 PedelFactory 不会与普通 Javascript 相撞。 如果您正在制作供第三方使用的公开 API, 它可能会与其他图书馆相撞, 不过 :

var MyApp = MyApp || {};
MyApp.__PanelFactory = function () { // ...
  • Get rid of the underscores (_) before all function names.

您不需要下划线, 因为与 JavaScript 合作一段时间的人知道每个对象(toString etc) 中包含的默认实例范围函数 :

MyApp.PanelFactory = function () {
  // ...
  function GetPanel(id) {
    //etc
  }
  • Name object constructor functions with initial-caps names, but name global or namespace-scoped functions and instance-scoped functions with initial-lower-case names:

这只是一个标准公约。它无助于区分内在功能,但正如我在前一个项目中所说,你不需要这样做。

MyApp.PanelFactory = function () {
  // ...
  function getPanel(id) {
    //etc
  }
  • Name local variables with initial-lower-case names:

这将帮助您辨别命名空间、 对象构造器和对象实例( 即您期待自定义实例范围的函数, 通常称为“ 方法 ” ) 之间的区别 。

var factory = new MyApp.PanelFactory();
var panel = factory.getPanel();
问题回答

以下是一些相当常见的公约 印在书本上。

Constants

通常在 All_CAPS_WITH_UNDERSCORES 中

var MAX_COUNT = 100;
var BASE_URL = "http://example.com/api/";

Variables

通常在下层的 CamelCase 中

var currentCount = 0;
var userName = "timrwood";

Constructors

通常在上卡梅尔Case

function Factory(){}
var factory = new Factory();

Methods

通常在下层的 CamelCase 中

Factory.prototype.getPanel = function(){};
var factory = new Factory();
var panel = factory.getPanel();

Private Variables/Methods

通常在 _lowerCamelCase with Awundscore Prefix 中使用;

Factory.prototype._fetchPanel = function(){};
Factory.prototype.getPanel = function() {
    return this._fetchPanel();
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签