如何在JSDOC中正确定义this引用的内容?

问题描述 投票:0回答:1

我这次来是带着一个非常简单的问题。在 JSDOC 上,有没有一种方法可以描述

this
在类中代表什么?

/**
 * 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();

javascript documentation jsdoc
1个回答
0
投票

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();
© www.soinside.com 2019 - 2024. All rights reserved.