相关疑难解决方法(0)

带有getter和setter的JavaScript类导致RangeError:超出了最大调用堆栈大小

我目前正在试验ECMA6课程.我目前的课程如下所示

class Player {
  constructor(id) {
    this.id = id;
    this.cash = 350;
  }

  get cash() {
    return this.cash;
  }

  set cash(value) { // line 19
    this.cash = value; // line 20
  }
};
Run Code Online (Sandbox Code Playgroud)

当我现在通过调用创建一个新的对象时,let playerObject = new Player(1);我收到以下错误

...\node_modules\mysql\lib\protocol\Parser.js:82
        throw err;
              ^
RangeError: Maximum call stack size exceeded
    at Player.cash (player.js:19:11)
    at Player.cash (player.js:20:15)
    at Player.cash (player.js:20:15)
    at Player.cash (player.js:20:15)
    at Player.cash (player.js:20:15)
    at Player.cash (player.js:20:15)
    at Player.cash (player.js:20:15)
    at Player.cash (player.js:20:15)
    at Player.cash (player.js:20:15)
    at Player.cash (player.js:20:15) …
Run Code Online (Sandbox Code Playgroud)

javascript node.js node-mysql

23
推荐指数
3
解决办法
1万
查看次数

标签 统计

javascript ×1

node-mysql ×1

node.js ×1