Skip to content

How To Document Namespace That Is Also a Function #955

@michaeldrotar

Description

@michaeldrotar

I came across an old issue that was similar to mine but seemed to have gotten derailed and closed for not having enough info.. so to hopefully provide better information with a real world scenario (comments shortened for brevity)..

/**
  My lib, which does many different things.
  @namespace
*/
var lib = {};

/**
  Provides ajax functionality

  @alias lib.http
  @namespace
*/
lib.http = lib.namespace('http'); // Create the http namespace to hold my http functions

/**
  Performs a highly configurable http request.
  @alias lib.http
  @method
*/
lib.http = lib.namespace('http', wrap($.http)); // Wrap jQuery's http method inside of a
// Promise and have the method throw actual errors.. by the definition of my namespace function,
// this wrapped function takes the place of the object that previously defined the namespace
// since functions can still have properties and methods.. any children of the previous object
// get attached to this function instead

/**
  Performs a GET request
  @alias get
  @memberOf lib.http
*/
lib.http.get = wrap($.get); // Wrap jQuery's get method inside of a Promise, etc..

/**
  Performs a POST request
  @alias post
  @memberOf lib.http
*/
lib.http.post = wrap($.post); // Wrap jQuery's post method inside of a Promise, etc...

I realize that using a function as a namespace seems odd... I don't often do it.. but there's an elegance in this scenario that I like.

If I want to make an ajax request, I simply do..

lib.http('myUrl', { /* settings */ }).then(function(response) {
});

If I want the convenient shorthand, it's namespaced nicely for me...

lib.http.get('myUrl').then(function(response) {
});

Any alteration of the doc comments that I try.. either lib.http is a function and the get and post functions are not present.. or lib.http is a namespace and it's params and returns are not present.

It would be ideal to have everything on its own namespace page (shortened for brevity):

lib. http
Provides ajax functionality
Source: lib.http.js, line 5

(static) lib.http(url, settings) -> Promise
Performs a highly configurable http request.
Parameters:
Source lib.http.js, line 50
Throws:
Returns:

Methods

(static) get(url, settings) -> Promise
Performs a GET request
...

(static) post(url, settings) -> Promise
Performs a POST request
...

If not possible, having functions as children of functions would at least provide the documentation appropriately..

lib
My lib, which does many different things
Source: lib.http.js, line 5

Namespaces
one
two

Methods

(static) http(url, settings) -> Promise
Performs a highly configurable http request.
Parameters:
Source lib.http.js, line 50
Throws:
Returns:

(static) http.get(url, settings) -> Promise
Performs a GET request
...

(static) http.post(url, settings) -> Promise
Performs a POST request
...

Thanks for your time!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions