1

I have functions which are return objects. I wish to put their jsdoc definitions to see their property and methods on webstorm intellisense.

How do I have to write jsdoc of below functions?

function MyOtherFunc() {
  return { a:'for eg string', b:12 }
}

function MyFunc() {
  var prop = MyOtherFunc();

  function myMethod() {
    alert( 'my method' );
  }

  function myOtherMethod() {
    alert( 'my other method' );
  }

  // explicitly return public methods when this object is instantiated
  return {
    someMethod : myMethod,
    someOtherMethod : myOtherMethod
  };      
}
uzay95
  • 16,052
  • 31
  • 116
  • 182

2 Answers2

2

You should be able to accomplish what you want using @namespace, @memberof and @instance.

/**
 * @namespace MyFunc
 */
function MyFunc() {
  var prop = MyOtherFunc();

  /**
   * Does myMethody things
   *
   * @memberof MyFunc
   * @instance
   */
  function myMethod() {
    alert( 'my method' );
  }

  /**
   * Does myOtherMethody things
   *
   * @memberof MyFunc
   * @instance
   */
  function myOtherMethod() {
    alert( 'my other method' );
  }

  // explicitly return public methods when this object is instantiated
  return {
    someMethod : myMethod,
    someOtherMethod : myOtherMethod
  };      
}

If MyFunc has any static methods, you would just leave off @instance or explicitly mark it with @static.

/**
 * Say hello
 *
 * @param {string} [name] - Who to say hello to
 * @memberof MyFunc
 * @static
 */
MyFunc.helloWorld = function (name) {
  console.log('Hello ' + (name || 'world'));
}

In this case, using @memberof and @static should be optional. The parser should be able to figure that out on its own; but, you might want to explicitly use them anyway to make it obvious to anyone reading the code.

Useless Code
  • 12,123
  • 5
  • 35
  • 40
  • I've done this in the past but was recently struggling to remember how I did it before. Came across your question while trying to figure it out. :-) – Useless Code May 02 '17 at 07:56
0

This exact case is handled properly in WebStorm without JSDoc too, but you may use closure compiler type syntax for this:

/**
 * @return {{a: string, b: number}}
 */
function MyOtherFunc() {
    return { a:'for eg string', b:12 }
}
de1mar
  • 1,176
  • 10
  • 6