用jsdoc记录javascript构造函数的返回 [英] Documenting the return of a javascript constructor with jsdoc
本文介绍了用jsdoc记录javascript构造函数的返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个返回构造函数的javascript函数(请参见下面的代码示例).我将如何使用jsdoc的@returns标签对此进行记录. @returns {MyConstructor}似乎并不正确,因为这意味着我返回的是"MyConstructor"的实例,而不是构造函数本身,对吧?
I have a javascript function that returns a constructor (see code sample below). How would I document this with the @returns tag of jsdoc. It doesnt seem correct to do @returns {MyConstructor} because that implies I am returning an instance of "MyConstructor" rather than the constructor itself, right?
function MyConstructor() {
var self = this;
self.myFunction = function() {
return true;
};
self.getMyFunctionResult = function() {
return self.myFunction();
};
}
/**
* @returns {?} A constructor that will be instantiated
*/
function getConstructor() {
return MyConstructor;
}
var constructor = getConstructor();
var instance = new constructor();
推荐答案
您可以使用以下方法检查函数返回的类型:
You can check the types returned by your functions using:
console.log(typeof constructor, typeof instance); // function object
在文档中说:
/**
* Returns the sum of a and b
* @param {Number} a
* @param {Number} b
* @returns {Number} Sum of a and b
*/
function sum(a, b) {
return a + b;
}
http://usejsdoc.org/tags-returns.html
所以在您的示例中,它将是:
So in you example it would be:
/**
* Returns the MyConstructor class
* @returns {Function} MyConstructor class
*/
function getConstructor() {
return MyConstructor;
}
或者如果您正在创建Item的实例:
Or if you are creating an instance of an Item:
/**
* Returns an instance of the MyConstructor class
* @returns {Object} MyConstructor instance
*/
function getInstance() {
return new MyConstructor();
}
这篇关于用jsdoc记录javascript构造函数的返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文