Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Documentation for instance vs static method name paths in JSDoc

When documenting javascript methods, I am aware that using # in a name path denotes an instance method, like:

function Class() {}

/**
 * Class#method1
 */
Class.prototype.method1 = function () {}

However, I have also seen uses of ~ and .. What are those used for?

/**
 * Class~method2
 * Class.method3
 */

Are there other syntaxes that I should be aware of as well?

like image 245
kavun Avatar asked Aug 13 '14 15:08

kavun


People also ask

What is a Namepath?

When referring to a JavaScript variable that is elsewhere in your documentation, you must provide a unique identifier that maps to that variable. A namepath provides a way to do so and disambiguate between instance members, static members and inner variables.

How do you write comments in JSDoc?

Create JSDoc commentsPosition the caret before the declaration of the method/function or field to document, type the opening block comment /** , and press Enter . WebStorm generates a JSDoc comment with a list of parameters ( @param ) and return values ( @returns ), where applicable.

What is instance method in JavaScript?

An instance method is a function which is part of a class, and acts on an instance of that class. Let's imagine a BankAccount class: class BankAccount { constructor(owner, balance) { this.

Is JSDoc built in?

By default, JSDoc uses the built-in "default" template to turn the documentation into HTML. You can edit this template to suit your own needs or create an entirely new template if that is what you prefer. This command will create a directory named out/ in the current working directory.


1 Answers

You can see the details for variable/method naming conventions here.

The . is used to denote a class method (also known as a static method), not an instance method. This means that on instances of the class (things created with new) you cannot call the class method.

Example:

Class.foo = function () {
  // ...
};

var blah = new Class();
blah.foo();  // throws an error

Class.foo();  // actually calls the function

The ~ is used to denote an inner method (also known as a private method), which is one defined inside a method of the class using function. These types of methods are not typically accessible from outside the method, so it is rare that you will see these documented.

Example:

function Class() {
  // this function is not accessible outside of the constructor
  function inner() {
  }

  // unless we give it some other reference that is visible:
  this.accessInner = inner;
}

blah = new Class();
blah.inner();        // throws an error
Class.inner();       // also throws an error
blah.accessInner();  // will actually call inner
like image 146
robbrit Avatar answered Oct 11 '22 18:10

robbrit