VS Code 私有字段误报警告“变量已声明,但从未读取其值。”

-3 javascript visual-studio-code

存在误报警告,例如“声明了变量,但从未读取其值”。

class Account {
    // Public field(s) -> instances
    local = navigator.language;

    // Private field(s)
    #movements = [];
    #pin;

    constructor(owner, currency, pin) {
      this.owner = owner;
      this.currency = currency;
      this.#pin = pin;

      console.log(`Thanks for opening an account, ${owner}!`);
    }

    getMovements() {
      return this.#movements;`your text`
    }

    deposit(val) {
      this.#movements.push(val);
    }

    withdraw(val) {
      this.deposit(-val);
    }
}

const marko = new Account('Marko', 'EUR', 1111);
console.log(marko.pin);
Run Code Online (Sandbox Code Playgroud)

在构造函数中使用了像 #pin 这样声明的私有方法,但 VS Code 无法识别它的使用。

在此输入图像描述

Que*_*tin 6

错误提示“从未被读取”。

您引用的行 ( this.#pin = pin;) 不会读取它。它会写入它。

它看起来console.log(marko.pin);可能是尝试读取它,但它失败了,因为它是私有成员。