javascriptdocumentationjsdoc

How to propertly define what this references to in JSDOC?


I come this time with a very simple question. Is there a way, on JSDOC, to describe what this represents in a class?

/**
 * This is a very simplified version of what is going on
 *
 * I want to add something here to indicate that, inside this function, `this` === CalledClass, 
 * so autocomplete for parameters/etc work
 */
const unboundedFunction = function () { 
    console.log(this);
    this.parameter = 50;
}

class CalledClass {
  parameter = 'test';

  callOutsideFunction() {
    unboundedFunction.bind(this)();
  }

}

const obj = new CalledClass();
obj.callOutsideFunction();


Solution

  • https://jsdoc.app/tags-this

    /** @this CalledClass  */
    const unboundedFunction = function () { 
        console.log(this);
        this.parameter = 50;
    }
    
    class CalledClass {
      parameter = 'test';
    
      callOutsideFunction() {
        unboundedFunction.bind(this)();
      }
    
    }
    
    const obj = new CalledClass();
    obj.callOutsideFunction();