热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何用JavaScript记录返回-HowtodocumentreturninJavaScript

ImwritingmyownlibraryfortheprojectatworkforabrowserapplicationandIamhavingthesam

I'm writing my own library for the project at work for a browser application and I am having the same old problem deciding how to comment the code.

我正在为一个浏览器应用程序的项目编写我自己的库,在决定如何注释代码时也遇到了同样的问题。

I'm trying to follow the JsDoc syntax, but will probably continue the Google Closure Compiler way. I may end up using two @return and @returns tags in the documentation, just for portability sake (when I setup the auto-generation of the documentation).

我正在尝试遵循JsDoc语法,但是可能会继续使用谷歌闭包编译器。我可能会在文档中使用两个@return和@returns标记,这只是为了便于移植(当我设置文档的自动生成时)。

Now, the question, how do you document the return of a custom anonymous object from a function? For example:

现在的问题是,如何从函数中记录自定义匿名对象的返回?例如:

return {
    username: 'username',
    password: 'password',
    enabled:  true
};

JsDoc has an example of how a @param can be documented to expect object with certain fields, but not the @returns tag. Similarly, the Google Closure Compiler documentation of a Record Type is vague and has no example to work it out.

JsDoc提供了一个例子,说明如何将@param文档化以期望具有特定字段的对象,而不是@returns标记。类似地,记录类型的谷歌闭包编译器文档是模糊的,没有示例来解决它。

3 个解决方案

#1


13  

The Closure-compiler uses a subset of the JSDoc annotations (and adds a few of its own). See the annotation reference for the compiler for the complete set. A JSDoc annotation is similar to a JavaDoc annotation and is a comment block that begins with /** (two stars). While each line of the comment often begins with it's own *, that is a convention that is not required. Only one JSDoc tag is allowed per line, but the arguments for a tag can span multiple lines.

Closure-compiler使用JSDoc注释的一个子集(并添加一些自己的注释)。查看完整集的编译器的注释引用。一个JSDoc注释类似于JavaDoc注释,是一个以/**(两颗星)开头的注释块。尽管注释的每一行通常以它自己的*开头,但这是一个不需要的约定。每行只允许有一个JSDoc标记,但是标记的参数可以跨多行。

The annotation typically applies to the following statement. Here are some examples:

注释通常应用于以下语句。下面是一些例子:

Variable
/** @type {string} */ var a;
Type Cast
var b = /** @type {string} */ (window['foo']);

note the extra parenthesis

注意额外的括号

Named Function
/**
 * @param {string} bar
 * @return {boolean}
 */
function foo(bar) { return true; }
Function Expressions
/** @type {function(string):boolean} */
var foo = function(bar) { return true; }

var foo2 =
  /**
   * @param {string} bar
   * @return {boolean}
   */
  function(bar) { return true; }
Typedef

Complex types (including unions, and record types) can be aliased for convenience and maintainability using a typedef. These annotations can be long, but can be split over multiple lines for readability.

使用typedef可以为方便和可维护性提供复杂类型(包括工会和记录类型)。这些注释可以很长,但是为了可读性,可以在多行中进行分割。

/** @typedef {{
 *             foo:string,
 *             bar:number,
 *             foobar:number|string
 *           }}
 */
var mytype;

For your original example, there are several possible ways to annotate such a function return value. One of the most specific and still convenient is the record type:

对于您的原始示例,有几种可能的方法来注释该函数的返回值。其中最具体也是最方便的是记录类型:

/** @return {{username:string, password:string, enabled:boolean}} */
function() {
  return {
    username: 'username',
    password: 'password',
    enabled:  true
  }
}

Note the extra {}. Also keep in mind that record types will not prevent property renaming.

注意额外的{ }。还要记住,记录类型不会阻止属性重命名。

This annotation tells the compiler that the function returns an anonymous type with username, password and enabled properties. Other valid options would be to define an interface elsewhere and typecast the return value to be that interface. The least specific annotation would be Object or *.

该注释告诉编译器该函数返回带有用户名、密码和启用属性的匿名类型。其他有效的选项是在其他地方定义接口,并将返回值类型转换为该接口。最不特定的注释将是Object或*。

To see a wide range of possible annotations, take a look at the extern files in the Compiler project.

要查看大量可能的注释,请查看编译器项目中的外部文件。

#2


3  

One of the nice and portable ways to do it is like the following, using return as a keyword. https://github.com/senchalabs/jsduck/wiki/%40return

一种很好的、可移植的方法是使用return作为关键字,如下所示。https://github.com/senchalabs/jsduck/wiki/%40return

/**
 * @return {object} return An object with the following properties
 * @return {string} return.username Some username
 * @return {string} return.password Some password
 * @return {boolean} return.enabled Is the user enabled?
 */
function getObj () {
     return {
         username: 'username',
         password: 'password',
         enabled:  true
      };
}

If you have to use it in multiple places, you would have to duplicate it, or use @typedef, but I haven't figured out how to add comments to @typedef so I use something like the following

如果必须在多个地方使用它,就必须复制它,或者使用@typedef,但是我还没有找到如何向@typedef添加注释的方法,所以我使用如下内容

/**
 * @typedef {username:string, password:string, enabled:boolean} MyType
 *  
 *  username: The name of the user 
 *  password: Some password
 *  enabled: Is the user enabled?
 */

/**
 * @return {MyType}
 */
function getObj () {
     return {
         username: 'username',
         password: 'password',
         enabled:  true
      };
}

You can also try the suggestion here How can I document a type in webstorm using just jsdoc?

您也可以在这里尝试建议,如何使用jsdoc在webstorm中记录类型?

#3


1  

If put this in top of the function

如果把它放在函数的上面。

function myFunction() {
    /**
     * Description of my function
     * @return {!Object.} Returns an object containing username, password and enabled information
     */

    // Do stuff
    return {
        username: 'username',
        password: 'password',
        enabled:  true
    }
}

推荐阅读
  • 本文介绍了闭包的定义和运转机制,重点解释了闭包如何能够接触外部函数的作用域中的变量。通过词法作用域的查找规则,闭包可以访问外部函数的作用域。同时还提到了闭包的作用和影响。 ... [详细]
  • vue使用
    关键词: ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • PHP图片截取方法及应用实例
    本文介绍了使用PHP动态切割JPEG图片的方法,并提供了应用实例,包括截取视频图、提取文章内容中的图片地址、裁切图片等问题。详细介绍了相关的PHP函数和参数的使用,以及图片切割的具体步骤。同时,还提供了一些注意事项和优化建议。通过本文的学习,读者可以掌握PHP图片截取的技巧,实现自己的需求。 ... [详细]
  • Commit1ced2a7433ea8937a1b260ea65d708f32ca7c95eintroduceda+Clonetraitboundtom ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
author-avatar
潘巧军_837
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有