简体   繁体   English

JavaScript中代码注释的正确方法是什么?

[英]What is the correct way of code comments in JavaScript

What is the correct way of code comments in Javascript - is the same syntax as in Java? Javascript中代码注释的正确方法是什么 - 与Java中的语法相同? And which tools actually would take advantage of these comments: 哪些工具实际上会利用这些评论:

  /*
  * Add an element to the group
  * @param {Object}  overlayElement
  * @param {Object} [element2] optional element
  */ 

I found new Resharper 6 (I write JS in VisualStudio 2010) offers the same comments as in C#, but only within the functions body, something like /// <param name="overlayElement"></param> . 我发现新的Resharper 6(我在VisualStudio 2010中编写JS)提供了与C#相同的注释,但仅在函数体内,类似于/// <param name="overlayElement"></param> The JS code comments are not highlighted as such by ReSharper. ReSharper没有突出显示JS代码注释。

What is the best way to go ...? 什么是最好的方式...?

using // is better than /* */ because then you can use the latter to take out an entire block containing other comments. 使用///* */更好,因为你可以使用后者取出包含其他注释的整个块。 However, if you want to use an automatic documentation generation tool, you must use comments similar to javaDoc style. 但是,如果要使用自动文档生成工具,则必须使用类似于javaDoc样式的注释。

This is an example that would work with YUI DOC (best one) https://yui.github.io/yuidoc/ 这个例子适用于YUI DOC(最好的一个) https://yui.github.io/yuidoc/

/**
* This is a description
* @namespace My.Namespace
* @method myMethodName
* @param {String} some string
* @param {Object} some object
* @return {bool} some bool
*/

good practice is to use // instead of /* */ 好的做法是使用//而不是/* */

The reason for that is because if you have */ in any part of the comment (assuming you do not intend to end yet), it would end the comment. 原因是因为如果你在评论的任何部分都有*/ (假设你还没有打算结束),它就会结束评论。 This happens even if */ is in a string. 即使*/在字符串中也会发生这种情况。 ie "*/" <--- this would end the comment and would likely to give you a syntax error. "*/" <---这将结束评论,并可能会给你一个语法错误。

note // ends at a line break so you would need // for every line of comment. 注意//以换行符结束,因此您需要//为每一行注释。

A good example is the Java based commenting still, which is also used with JSDoc. 一个很好的例子是基于Java的评论,它也与JSDoc一起使用。 You can find examples here: http://code.google.com/p/jsdoc-toolkit/wiki/FAQ 您可以在此处找到示例: http//code.google.com/p/jsdoc-toolkit/wiki/FAQ

To add simple onliners as comments, the // is still a good way to comment your code. 要将简单的在线人员添加为评论,//仍然是评论代码的好方法。 But for generating documentation, I'd go with the JSDoc syntax. 但是为了生成文档,我会使用JSDoc语法。 I have used it in the past and it works quite well. 我过去曾经使用它,效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Javascript中实现解耦代码/回调的正确方法是什么? - What is the correct way to implement decoupled code / callbacks in Javascript? 在Javascript,CSS或HTML代码中的注释中隐藏或加密信息的最佳方法是什么? - What is the best way of hiding or encrypting information in comments in Javascript, CSS or HTML code? 在“样式”和“onclick”等属性中编写注释的正确方法是什么? - What's the correct way to write comments in attributes such as 'style' and 'onclick'? 使用 javascript .onclick 函数的正确方法是什么? - What is the correct way to use the javascript .onclick function? 修复此 Javascript 闭包的正确方法是什么 - What is the correct way to fix this Javascript closure 使用 Javascript 编写 HTML 的正确方法是什么? - What is the correct way to write HTML using Javascript? 实现此 Javascript 循环的正确方法是什么? - What is the correct way to achieve this Javascript loop? 调用JavaScript函数的正确方法是什么? - What's the correct way to call JavaScript Function? JavaScript - 在RegExp中用“/”替换“\\”字符的正确方法是什么 - JavaScript - What is correct way for replace “\” charcter with “/” in RegExp 将CSS / Javascript链接到网页的正确方法是什么? - What is the correct way to link the CSS/Javascript to webpage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM