0

When creating an object from constructor, sometimes the result object have too much property to display when using console.log. How can I specify which property to display?

For example if I have a constructor like this:

function Obj(source) {
  this.lines = source.split('\n'); // it can be 1000 lines or even more
}

So, If I do:

var obj = new Obj(source); console.log(obj);

it will print all those lines to the console. I want to exclude that property on console.log, how?

5
  • 1
    "it can be 1000 lines or even more".. Are you talking about split data ? Or properties of the Obj ? Commented Apr 27, 2016 at 11:10
  • You can specify which properties to print out by doing console.log(obj.lines) or console.log(obj.some_property) Commented Apr 27, 2016 at 11:11
  • @Rayon i'm talking about the object instance which will have that lines as property and which will fill up the output Commented Apr 27, 2016 at 11:17
  • What environment are you working in, which console are you using? Commented Apr 27, 2016 at 11:19
  • @Bergi mainly nodejs environment, but I plan to make it works on browser too. What I'm looking for is maybe some sort of __repr__ method in python, which is not toString, because toString is __str__ equivalents. Commented Apr 27, 2016 at 11:21

5 Answers 5

1

On nodejs, console.log does use util.inspect to format objects for printing. You can customise it by providing your own inspect method on your objects.
Another simple way to prevent a property from being printed to the node console is to make it non-enumerable, as long as that doesn't break your code.

In browsers, with their interactive inspection of logged objects, you usually don't have a problem with too-large objects, as they will be expanded only on request. If you want to control exactly what is printed, your only option is to pass a string to console.log, though.

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

Comments

0

Other than using something else, no, you can't reasonably limit what console.log shows, and what it shows varies from console implementation to console implementation (or even within the same implementation depending on whether you're showing the console or not when log is called).

You may find console.dir more useful in this case than console.log. From MDN:

Displays an interactive list of the properties of the specified JavaScript object. The output is presented as a hierarchical listing with disclosure triangles that let you see the contents of child objects.

Of course, that's only really useful if you have an interactive console display (such as in a browser).

Alternately, you could use console.log(String(obj)); and override the default toString to do what you want:

Obj.prototype.toString = function() {
    // generate your designer output here
    return desiredOutputString;
};

Depending on the console implementation, in some cases, you may not need the String(...) part when calling console.log, but in most of them you would.

Comments

0

It will by default print all the properties. Unless you specify which property you want to print, it will print all.

If you don't want all the lines then define the property as a function which will give line by line view

function Obj(source) {
  this.source = source;
  this.lines = function(){
     return this.source.split('\n');
  } 
}

Now if you do console.log(Obj) it will only show one line.

1 Comment

Of course, this fundamentally changes the objects, which I tend to doubt is an option.
0

Add a method which will print the object as you like. E.g.

Obj.prototype.debug = function () {
  console.log({
    x: this.x,
    y: this.y
  });
};

Comments

0

If it's just in the logging that you want to omit the property you could define and immediately call a function in your console.log command.

The function could copy the object, remove the lines property from it and log the resulting object:

function Obj(source) {
  this.lines = source.split('\n'); // it can be 1000 lines or even more
}
var obj = new Obj(source); 
console.log((function(){var copy = Object.assign({}, obj); delete copy.lines; return copy;})())

Comments

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.