admin管理员组

文章数量:1305158

How can I document a constructor (function) passed as a parameter?

Example:

/** @class */
function A() {}

/**
 * @param {Function} aConstructor
 */
function createA(aClass) {
    return new aClass();
}

As you can see, I can specify that it is a function. However, I can't specify which object that function would create. Is there some way to document this?

Thanks.

How can I document a constructor (function) passed as a parameter?

Example:

/** @class */
function A() {}

/**
 * @param {Function} aConstructor
 */
function createA(aClass) {
    return new aClass();
}

As you can see, I can specify that it is a function. However, I can't specify which object that function would create. Is there some way to document this?

Thanks.

Share Improve this question asked Jun 16, 2013 at 1:59 samanimesamanime 26.6k17 gold badges97 silver badges150 bronze badges 5
  • Why not under @returns like @returns {A} or if multiple types are possible, you could do @returns {A|B}}? – Brett Zamir Commented Jun 16, 2013 at 2:20
  • It appears you may be able to do this with @constructs <name> (see the 2nd example at usejsdoc/tags-constructs.html ) – Brett Zamir Commented Jun 16, 2013 at 2:29
  • In this exact case, I would indeed return that data type. However, that doesn't strictly denote that it is the expected data type of the parameter itself. Same with your other example. – samanime Commented Jun 16, 2013 at 3:15
  • I'd file a request because it doesn't appear to me to support that capability. Using the approach of instanceof, "Function" is the closest for the time being... – Brett Zamir Commented Jun 16, 2013 at 7:42
  • After hunting, I think you are correct. If you want to post that as answer, I'll accept it. – samanime Commented Jun 16, 2013 at 8:46
Add a ment  | 

2 Answers 2

Reset to default 9

Google with its closure propose a {function(new:type)} as a type description. I've assumed, that one can use something like this (I'm using it with AMD):

 /** @param {function(new:ClassOrInterfaceName)} aClass */

You can find more information here: http://google-styleguide.googlecode./svn/trunk/javascriptguide.xml#JavaScript_Types

I hope, my answer can help someone )

As discussed in the ments, I'd file a request because it doesn't appear to me to support that capability. Using the approach of instanceof, "Function" is the closest in representing the object instance for a constructor for the time being. As mentioned, @constructs or @returns may be able to help you to indicate the output, but not the parameter input.

本文标签: javascriptJSDoc 3 Document constructorclass parameterStack Overflow