Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an __repr__ equivalent for javascript?

The closest I got to something close to Python's repr is this:

function User(name, password){          this.name = name;          this.password = password; } User.prototype.toString = function(){     return this.name; };    var user = new User('example', 'password');  console.log(user.toString()) // but user.name would be even shorter 

Is there a way to represent an object as a string by default? Or am I going to have to just use object.variable to get the results I want?

like image 408
user3234209 Avatar asked Jul 23 '14 04:07

user3234209


People also ask

Does repr return a string?

TrueNow, according to the official documentation by Python, repr() returns a string that holds a printable representation of an object. For most types in Python, this will give you a string that yields an object with the same value as when we pass it to eval().

What is a repr method?

The __repr__ method returns the string representation of an object. Typically, the __repr__() returns a string that can be executed and yield the same value as the object. In other words, if you pass the returned string of the object_name.

What is __ repr in Python?

Python __repr__() function returns the object representation in string format. This method is called when repr() function is invoked on the object. If possible, the string returned should be a valid Python expression that can be used to reconstruct the object again.


2 Answers

JSON.stringify is probably the closest you are going to get from native libraries. It doesn't work well with objects, but you could define your own code to work around that. I searched for libraries that provide this functionality but didn't find anything.

like image 174
Andrew Johnson Avatar answered Sep 23 '22 08:09

Andrew Johnson


Node.js util.inspect

http://nodejs.org/api/util.html#util_util_inspect_object_options

To get the object representation that appears on the terminal as a string you can do:

const util = require('util'); console.log(util.inspect({ a: "0\n1", b: "c"})); // Equivalent: automatically called by `console.log`. console.log({ a: "0\n1", b: "c"}); 

output:

{ a: '0\n1', b: 'c' } { a: '0\n1', b: 'c' } 

This is the same that would appear on the terminal if you copy pasted the string { a: "0\n1", b: "c"} into the terminal and pressed enter.

Override the inspect method of a class for a custom representation

This was asked at: How to change string representation of objects in Nodejs debug console view and mentioned at: https://stackoverflow.com/a/32866283/895245 and https://stackoverflow.com/a/54667225/895245 but here goes a fuller example:

const util = require('util');  class MyClass {   constructor(a, b) {     this.a = a;     this.b = b;   }   [util.inspect.custom]() {     return `a is ${this.a} and b is ${this.b}`;   } }  const my_object = new MyClass(1, 2); console.log(util.inspect(my_object)); console.log(my_object); 

Output:

a is 1 and b is 2 a is 1 and b is 2 

The default inspect if we hadn't defined a custom [util.inspect.custom] would have been:

MyClass { a: 1, b: 2 } 

That same representation also shows if you just directly inspect the object on a terminal:

> my_object a is 1 and b is 2 

In older Node.js, the syntax to define the custom printer used to be just:

inspect() {   return `a is ${this.a} and b is ${this.b}`; } 

but that was deprecated with message:

[DEP0079] DeprecationWarning: Custom inspection function on Objects via .inspect() is deprecated 

as mentioned at:

  • (node:71307) [DEP0079] DeprecationWarning
  • https://nodejs.org/api/util.html#util_util_inspect_custom
  • https://nodejs.org/api/deprecations.html#deprecations_dep0079_custom_inspection_function_on_objects_via_inspect

Pretty print util.inspect with newlines and indentation

Not possible? Sigh:

  • https://github.com/nodejs/node/issues/14638
  • Indented, multi-line logging in NodeJS

One big advantage over JSON.stringify is that inspect takes care of circular dependencies for you. But without pretty print, it is a pain.

Tested in Node.js v10.15.1.