js中get set 的用处体现在哪里?死循环?

var cart = {

  _wheels: 4,


  get _wheels () {

    return this._wheels;//Maximum call stack size exceeded

  },


  set _wheels (value) {

    if (value < this._wheels) {

      throw new Error('数值太小了!');

    }

    this._wheels = value;

  }

}

console.log(cart._wheels);

cart._wheels = 1;

console.log(cart._wheels);


守候你守候我
浏览 574回答 1
1回答

宝慕林4294392

建议看看这篇文章:关于JavaScript中Get/Set访问器然后就是,我把你这段代码复制进我的 VS Code 中时,显示是这样的:所以,一个好的编辑器能够帮你直接发现问题。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript