Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to document this keyword in custom components with jsDoc?

I'm using Vuejs inline template components where we register the component in a javascript file and the template in html.

the component looks something like this:

Vue.component('compare-benefits', {
  data() {
     // the "this" keyword in methods should refer to this object
     return {
        ...state,
        isLoading: false,
     }
  },
  methods: {
    getData() {
       // I want the "this" keyword here to reference the object return in data above 
       this.isLoading = true;
    }
  }
})

If you are not familiar with Vue, whats happening here is that Vue framework will bind the this keyword in your methods to the object you return from the data() method.

How do I use jsDoc here and tell it that the this keyword here is in fact referencing that object?

EDIT: Primary reason for using jsDoc is not to create documentation but rather to have intellisense and type checking in vscode using @ts-check

like image 605
tito.300 Avatar asked Aug 05 '20 17:08

tito.300


People also ask

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.

Does JSDoc work with TypeScript?

You can use most JSDoc type syntax and any TypeScript syntax, from the most basic like string to the most advanced, like conditional types.

Is there Javadoc for JavaScript?

JSDoc 3 is an API documentation generator for JavaScript, similar to Javadoc or phpDocumentor. You add documentation comments directly to your source code, right alongside the code itself. The JSDoc tool will scan your source code and generate an HTML documentation website for you.

What is VuePress?

VuePress is composed of two parts: a minimalistic static site generator with a Vue-powered theming system and Plugin API, and a default theme optimized for writing technical documentation. It was created to support the documentation needs of Vue's own sub projects.


1 Answers

The this keyword in Vue framework is an object of type Vue. You can identify it by debugging your code within your getData method or any other method. Moreover, the Vue data are properties to this. I have uploaded a screenshot below for you to see it from an example of my own that I am currently working on:

enter image description here

As a result, your code after jsDoc usage will be like this:

Vue.component('compare-benefits', {
    data() {
        return {
            ...state,
            isLoading: false,
        }
    },
    methods: {
        /** @this Vue */
        getData() {
            this.isLoading = true;
        }
    }
})
like image 185
vchan Avatar answered Nov 15 '22 05:11

vchan