RangeError:在Node.js中调试/记录/检查对象时超过了最大调用堆栈大小。
Given a class definition as below, I am getting RangeError: Maximum call stack size exceeded
when trying to see which properties the Object has.
给定一个类定义如下,我得到了RangeError:在尝试查看对象的属性时超出了最大调用堆栈大小。
var Person = (function () {
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.inspect = function () {
console.log(this);
};
return Person;
})();
var radek = new Person("Radek", 28);
radek.inspect();
In browser (Chrome), we will get the following though:
在浏览器(Chrome)中,我们将得到以下内容:
Person {name: "Radek", age: 28, inspect: function}
1 个解决方案
#1
3
Funny you should ask. By default custom inspect()
functions defined on the objects being inspected will be called when we try to inspect them. This leads into a recursion with no end in our case.
有趣的你应该问。当我们试图检查对象时,默认的自定义检查()函数将被调用。在我们的例子中,这导致了一个没有结束的递归。
To alleviate the problem while preserving the name use the util
module passing an extra option customInspect
into inspect()
:
为了缓解这个问题,同时保留这个名称,使用util模块通过一个额外的选项自定义检查():
var util = require("util");
var Person = (function () {
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.inspect = function () {
console.log(util.inspect(this, { 'customInspect': false }));
};
return Person;
})();
var radek = new Person("Radek", 28);
radek.inspect();
Which will give us the following:
这将给我们带来以下几点:
{ name: 'Radek', age: 28 }
{姓名:'Radek',年龄:28}
更多相关文章
- 是否有一个简单的库将JSON对象渲染为树?
- 在JavaScript中进行文件处理,第四部分:对象URLs
- 我在显示随机选择的对象时遇到问题
- 如何在JavaScript / jQuery中获取对象的属性?
- 【JavaScript】JavaScript的对象-对象专门语句
- 令人惊奇的JavaScript面向对象(一)
- JavaScript面向对象程序设计三——原型模式(上)
- 迭代angularjs中对象中的属性列表
- Safari / Chrome中的全局控制台对象被重置