注释javascript对象和方法的首选方法是什么
时间:2020-03-06 14:39:40 来源:igfitidea点击:
我习惯使用Atlas的首选方法(据我所知)是使用xml注释,例如:
/// <summary> /// Method to calculate distance between two points /// </summary> /// /// <param name="pointA">First point</param> /// <param name="pointB">Second point</param> /// function calculatePointDistance(pointA, pointB) { ... }
最近,我一直在研究其他第三方JavaScript库,并且看到如下语法:
/* * some comment here * another comment here * ... */ function blahblah() { ... }
另外,请告诉我是否有JavaScript的API生成器可以读取"首选"注释样式。
解决方案
在第一个示例中,三元注释的使用实际上用于外部XML文档工具和(在Visual Studio中)智能感知支持。它仍然是有效的注释,但是它的特殊之处是:)实际注释'operator'是//
唯一的限制是它仅适用于单行。
第二个示例使用C样式块注释,该注释允许跨多行或者在一行的中间进行注释。
有JSDoc
/** * Shape is an abstract base class. It is defined simply * to have something to inherit from for geometric * subclasses * @constructor */ function Shape(color){ this.color = color; }
尝试将以下内容粘贴到Visual Studio 08中的javascript文件中并对其进行处理:
var Namespace = {}; Namespace.AnotherNamespace = {}; Namespace.AnotherNamespace.annoyingAlert = function(_message) { /// <param name="_message">The message you want alerted two times</param> /// <summary>This is really annoying!!</summary> alert(_message); alert(_message); };
Intellisense嘉豪!
可以在Scott Gu的博客上找到有关此问题的更多信息(包括如何在大型库中使用外部javascript文件)。
雅虎提供YUIDoc。
它有充分的文档记录,受到Yahoo的支持,并且是一个Node.js应用程序。
它也使用了很多相同的语法,因此从一个到另一个就不必进行太多更改。