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}

更多相关文章

  1. 是否有一个简单的库将JSON对象渲染为树?
  2. 在JavaScript中进行文件处理,第四部分:对象URLs
  3. 我在显示随机选择的对象时遇到问题
  4. 如何在JavaScript / jQuery中获取对象的属性?
  5. 【JavaScript】JavaScript的对象-对象专门语句
  6. 令人惊奇的JavaScript面向对象(一)
  7. JavaScript面向对象程序设计三——原型模式(上)
  8. 迭代angularjs中对象中的属性列表
  9. Safari / Chrome中的全局控制台对象被重置

随机推荐

  1. Android中MaterialDesign使用 (四)Coordi
  2. Android(安卓)系统字体规范与应用探索
  3. Android(安卓)中的ellipsize
  4. Android4.4系统源代码百度网盘下载
  5. 读书笔记(一)---GoogleAndroid开发入门指南
  6. android中去掉标题栏和状态栏
  7. 【Android學習專題】搭建Android NDK环境
  8. Android 5.1源代码与Nexus设备工厂镜像下
  9. RatingBar
  10. android timed gpio (linux 3.0.0) 受时