2016-12-15 33 views
0

我想知道如何文档的功能如下所示:JSDocs - 如何记录隐含参数,并返回

var createInput = function() { 
    var type = this.type; 

    this.input = { 
    val: {}, 
    type: type 
    }; 
} 

技术上this.type的输入参数和this.input是什么,是从该对象返回。如何记录这一点?

回答

-1

最好重写一遍代码,这样它就可以获取参数并返回一些没有任何副作用的值。易于记录!

的问题应该是更像如何避免副作用,而不是如何记录副作用

var createInput = function (type) { 
    return { 
    val: {}, 
    type: type 
    }; 
} 

this.input = createInput(this.type); 
+0

原则上,我有些同意,但我们在代码中使用了许多具有隐式共享变量的层次结构。我基本上需要在每个函数中发送'context'作为参数,尽管我可以通过'this'直接访问它。 我认为这个问题可以写成:如何在JSDocs中记录类变量? – PunkCode

+0

你的意思是'对象'作为参数? – sabithpocker

+1

检查[this](http://usejsdoc.org/tags-param.html#parameters-with-properties)和[this](http://usejsdoc.org/tags-typedef.html) – sabithpocker

0

sapithpocker在第二个链接上跳过似乎显示出一个潜在的解决方案。

/** 
* The context of the current class. 
* @typedef {Object} ClassContext. 
* @property {string} type - Indicates type. */ 

/** 
* A method for doing something. 
* @param {...ClassContext} this - a {@link ClassContext} object. 
*/ 
function createInput() { /* Code */ } 

这是说明文件,对于每个使用对象“拥有”的当前上下文,即this方法可以重复使用,并且遵循DRY-原理,以及。