当前位置:  开发笔记 > 编程语言 > 正文

ES6 Class超出最大调用堆栈大小

如何解决《ES6Class超出最大调用堆栈大小》经验,为你挑选了1个好方法。

我正在涉及新的ES6 Class关键字,我有一个时髦的场景,给我一些无限循环,最终Maximum call stack size exceeded.

这是我的班级:

'use strict';

class Group {

 set name(newName) {
    this.name = newName;
  }

}

module.exports = Group;

我用这个摩卡测试来调用它:

'use strict';

const expect = require('chai').expect;

describe('Group model', function() {

  var Group;

  it('should not blow up when requiring', function() {
     Group = require('../../models/group');
     expect(Group).to.not.be.undefined;
  });

  describe('create()', function() {
    it('should fail with no parameters', function() {
      const expected = new Error();

      var group = new Group();
      //group.name('myName');
      group.name = 'myName';

    });

  });

});

当我运行我的摩卡测试时,我得到这个打印输出:

 1) Group model create() should fail with no parameters:
     RangeError: Maximum call stack size exceeded
      at onwrite (_stream_writable.js:335:15)
      at WritableState.onwrite (_stream_writable.js:89:5)
      at WriteStream.Socket._writeGeneric (net.js:684:5)
      at WriteStream.Socket._write (net.js:694:8)
      at doWrite (_stream_writable.js:292:12)
      at writeOrBuffer (_stream_writable.js:278:5)
      at WriteStream.Writable.write (_stream_writable.js:207:11)
      at WriteStream.Socket.write (net.js:618:40)
      at Console.log (console.js:36:16)
      at Group.name (models/group.js:122:13)
      at Group.name (models/group.js:123:15)
      at Group.name (models/group.js:123:15)
      at Group.name (models/group.js:123:15)
      at Group.name (models/group.js:123:15)
      at Group.name (models/group.js:123:15)
      at Group.name (models/group.js:123:15)
      at Group.name (models/group.js:123:15)
      at Group.name (models/group.js:123:15)
      at Group.name (models/group.js:123:15)
      at Group.name (models/group.js:123:15)
      at Group.name (models/group.js:123:15)
      at Group.name (models/group.js:123:15)

造成这种无限循环的原因是什么?

仅供参考我在node.js 5.1.0上



1> Ixonal..:

你的setter属性以递归方式设置自己.尝试更多的东西......

class Group {

 set name(newName) {
    this._name = newName;
  }

  get name() {
    return this._name;
  }

}

推荐阅读
wurtjq
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有