java中的多行注释符号,JavaScript多行注释中的“ @”符号有什么作用?

Just really curious after I was poking around the Muuri source code and saw this everywhere:

var htmlCollectionType = '[object HTMLCollection]';

var nodeListType = '[object NodeList]';

/**

* Check if a value is a node list

*

* @param {*} val

* @returns {Boolean}

*/

export default function isNodeList(val) {

var type = Object.prototype.toString.call(val);

return type === htmlCollectionType || type === nodeListType;

}

@param and @returns don't seem to actually do anything (I think), but they ARE highlighted differently. In fact, if you look at the code in git they're highlighted as though they're not comments.

Is this some JavaScript syntax I'm unaware of? What's going on here? I would love to know.

解决方案

This is just utilisting JSDoc comments. The syntax is influenced by Java which has JavaDoc comments as part of the standard. In short, the comment is documenting what a function or method does and it has slightly special syntax - it is a block comment that starts with /** instead of merely /* to differentiate it from a normal block comment and you can use some annotations to denote different meanings:

@param means this is a parameter.

The value inside {} denotes the type of the parameter - in this case * means "any", but you to document something like @param {string} or @param {number}

the val is the name of the parameter that the function uses.

you can optionally add a description for the parameter e.g., something like @param {*} val - used for foo and bar

the @return documents the return of the function.

the value inside {} is the type again. In this case, a boolean.

you can still optionally add a comment for the return value, for example: @returns {Boolean} true if correct, false if incorrect

There are more things you can document using JSDoc syntax, like @copyright to specify a license or @throws to declare what are the expected exceptions that some code can throw. Some syntax is specific for functions or methods, other for objects or even whole files.

All in all, it's an attempt to standardise the descriptions left in files. You don't need to do anything with the comment but you can also use tools that read the comments and act on them - some like Tern.js will read the comments and try to check if your code conforms, e.g., if you have

/**

* @param {number} bar

* @return {boolean}

*/

function foo(bar) {}

and you call foo("abc") then you might get a warning by the tool that you should be passing a number. Or if you do foo(123).replace("a", "b") you can get a warning that you're trying to use string methods on what should be a boolean.

Other tools might instead just crawl your JS files and generate documentation. Java does this with JavaDoc - you can generate the documentation for your methods and classes automatically basing it on the JavaDoc comments. You would get a documentation in the official Java style which means any documentation will be consistent.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值