Monday 15 February 2010

javascript - RangeError: Maximum call stack size exceeded when debugging/logging/inspecting an Object in Node.js -


Looking at the definition of a square below, I get the RangeError: maximum call stack size When trying to see what the object is, to see what qualities are there.

  var person = (function () {function person (name, age) {this.name = name; this.age = age;} Person.prototype .intpect = function () {console .log (this);}; return person;}) (); Var radek = new person ("Radek", 28); Radek.inspect ();   

In the browser (Chrome), we will get the following:

  person {name: "rack", Age: 28, inspection: function}    

Fun should you ask by custom inspection () When we try to inspect them, we will be called upon to define the tasks defined on the items being inspected. This leads to a recursive with no end in our case.

Using util module to reduce the problem while preserving the name is giving additional options customInspect in inspection < / Code>:

  var util = require ("use"); Var person = (function () {function person (name, age) {this.name = name; this.age = age;} person.prototype.inspect = function () {console.log (util.inspect (this, { 'Custom inspect': false}})}} return person;}) var radek = new person ("Radek", 28); radek.inspect ();   

Who will give us:

{name: 'Radek', age: 28}

No comments:

Post a Comment