2

Given a class definition as below, I am getting RangeError: Maximum call stack size exceeded when trying to see which properties the Object has.

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:

Person {name: "Radek", age: 28, inspect: function}

1 Answer 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():

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 }

Sign up to request clarification or add additional context in comments.

1 Comment

+1 Funny that you ask and answer. Neat issue too... I could see some people grinding their head into sandpaper over this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.