azt*_*ack 44 javascript google-closure-compiler jsdoc coffeescript
我有一些用CoffeeScript编写的代码,我想用Google Closure Compiler优化生成的JavaScript,所以这些文件需要用JSDoc记录.
我的问题是,如何记录*.coffee文件以生成包含用于闭包编译器的工作JSDoc的javascript?
还有一个问题:有没有办法在*.coffee中保留单行注释?
Bil*_*oon 77
### define function variable before block to avoid code being appended to closing part of JSDoc comment ###
cube = null
###*
* Function to calculate cube of input
* @param {number} Number to operate on
* @return {number} Cube of input
###
cube = (x) -> x*x*x
Run Code Online (Sandbox Code Playgroud)
coffee -cpb src.coffee// Generated by CoffeeScript 1.6.3
/* define function variable before block to avoid code being appended to closing part of JSDoc comment*/
var cube;
cube = null;
/**
* Function to calculate cube of input
* @param {number} Number to operate on
* @return {number} Cube of input
*/
cube = function(x) {
return x * x * x;
};
Run Code Online (Sandbox Code Playgroud)
正如其他答案中详述的那样,CoffeeScript 1.7.1有更好的方法可以解决这个问题.
mik*_*att 33
由于我不能直接回复上面的Billy,似乎CoffeeScript 1.7.1对此有更好的支持:
###*
# Sets the language and redraws the UI.
# @param {object} data Object with `language` property
# @param {string} data.language Language code
###
handleLanguageSet: (data) ->
Run Code Online (Sandbox Code Playgroud)
输出
/**
* Sets the language and redraws the UI.
* @param {object} data Object with `language` property
* @param {string} data.language Language code
*/
handleLanguageSet: function(data) {}
Run Code Online (Sandbox Code Playgroud)
你必须进行实验(很多),但###评论是你的朋友.
咖啡脚本编译器将保留使用该###表单的注释(此处为 docs ).
我尝试JsDoc使用网站上的'try coffeescript'功能为函数创建一个非常简单的片段:
###* Doc for this function.###
foo = -> 'bar'
Run Code Online (Sandbox Code Playgroud)
这给了:
/** Doc for this function.
*/
var foo;
foo = function() {
return 'bar';
};
Run Code Online (Sandbox Code Playgroud)
我不是专家JsDoc,但我猜var foo;这个函数上面的语句会产生问题.如果您foo之前已经宣布过,也许..
听听它是怎么回事真好.
我建议不要这样做。对所有代码进行 JSDoc 编译是一个费力的过程,闭包编译器可能不会带来什么好处。除了谷歌本身之外,几乎没有人这样做。CoffeeScripters/JavaScripters 通常更喜欢轻量级文档工具,例如docco。
此外,虽然 Closure Compiler 背后有 Google 品牌名称,但 UglifyJS在许多情况下已被证明是更有效的缩小工具。(jQuery最近改用它。)
还有一个问题:有没有一种方法可以在 *.coffee 中保留单行注释?
是的:
### foo ###
Run Code Online (Sandbox Code Playgroud)
或者
`// foo`
Run Code Online (Sandbox Code Playgroud)