Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 467 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is there an __repr__ equivalent for javascript?

#1
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?*
Reply

#2
[`JSON.stringify`][1] 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.

[1]:

[To see links please register here]

Reply

#3
String(user)

Is the best I can think of. I think another alternative may be to find a 3rd party lib that handles creating human readable presentation for objects.
Reply

#4
As Andrew Johnson said, `JSON.stringify` is probably the closest you can get out of the box.

One common strategy for a repr is to [output runnable Python code](

[To see links please register here]

). If you want to do this, [lave](

[To see links please register here]

) (opposite of `eval`) is a good choice.

Example:

var escodegen = require('escodegen')
var lave = require('lave')

function User(name, password){
this.name = name;
this.password = password;
}

var user = new User('example', 'password');

console.log(lave(user, {generate: escodegen.generate}));

Output (not as elegant as I had hoped!):

var a = Object.create({ 'constructor': function User(name, password){
this.name = name;
this.password = password;
} });
a.name = 'example';
a.password = 'password';
a;
Reply

#5
A quick shortcut for me is to wrap the value with an array literal, like this:

console.log([variable]);

The output in the browser's developer console makes it quite clear what the only element of the array is.

[![Screenshot of developer console on Firefox][1]][1]


[1]:
Reply

#6
This is solution for NodeJS (not sure about browser). As [

[To see links please register here]

][1] says, you could add `inspect(depth, opts)` to your class and it will be called when you `console.log(user_class_instance);`

Therefore this should do the trick:

User.prototype.inspect = function(depth, opts){
return this.name;
};



[1]:

[To see links please register here]

Reply

#7
Node v6.6.0 introduced the [util.inspect.custom][1] symbol: it is a globally registered symbol accessible through `Symbol.for('nodejs.util.inspect.custom')`. It can be used to declare custom inspect functions.

Here's a usage example with OP's case:

<!-- language: lang-js -->

function User(name, password){
this.name = name;
this.password = password;
this[Symbol.for('nodejs.util.inspect.custom')] = () => this.name;
}

var user = new User('example', 'password');

console.log(user) // 'example'

<!-- end snippet -->


[1]:

[To see links please register here]

Reply

#8
In Nodejs, `console.log` representation of a Javascript object can be overridn by adding `inspect()` method to that Object

eg:

function User(name, password){
this.name = name;
this.password = password;
}
User.prototype.toString = function(){
return this.name;
};
User.prototype.inspect = function(){ return 'Model: ' + this.name ; }


-Thanks to 'Ciro Santilli'
Reply

#9
**Node.js `util.inspect`**

[To see links please register here]


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:

[To see links please register here]

and mentioned at:

[To see links please register here]

and

[To see links please register here]

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:

-

[To see links please register here]

-

[To see links please register here]

-

[To see links please register here]


**Pretty print `util.inspect` with newlines and indentation**

Not possible? Sigh:

-

[To see links please register here]

-

[To see links please register here]


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.
Reply

#10
# Yes, this is 100% possible with JavaScript!

You can Skip to the end for the solution directly or read this for a detailed explanation.

## The Approach

The solution depends on how you want to implement it as the runtime environment comes into play. Browsers implement runtime environments that enforce ECMAScript standards they adopt while Node.js is a fork on V8 and has it's own modules that deals with how standard

Below, I will take a use case and give two solutions and direct solution to the example problem of your question:

1. **An Universal Solution** that you can implement and it will work on the browser AND on Node.js
2. **Node.js** specific solution.
3. **Solution to the exact Use Case of OP**.

> **Note:** This will also help those studying Data Structures and trying to do a JavaScript implementation.

---

## The Problem

Let's take a Use Case similar to the OP's use case to understand this better. Suppose you are building the Array Data Structure from scratch as a custom **`xArray`** constructor. For simplicity let's only implement the `push` method so we can push some data and then try to output the result exactly the way the JavaScript Array constructor creates objects and the prints the array when we `console.log` the object or return it from a function.

We will also need the static method `XArray.isArray(input)` so we can test if the instance of the object is the same as the constructor.

If we simply `console.log` the Object without adding the solution, we will get something that looks like:

**Undesired Solution:**
```es6
const xArray = new XArray();
xArray.push('a', 'b', 'c', 'd', 'e');
console.log(xArray);
// Prints: XArray { '0': 'a', '1': 'b', '2': 'c', '3': 'd', '4': 'e' }
```

But that's not what we want. We need the `Object.values(xArray)` just like in the OP's use case, `user.name` is required as the output and not the whole `User { name: 'example', password: 'password' }`

So, instead, this is the output we want:

**Desired Solution:**
```es6
// Test function that takes an array and returns it.
const returnArr = array => array;

/************************************************************************************************/
/** Array using custom XArray constructor **/
/************************************************************************************************/
const xArray = new XArray();
xArray.push('a', 'b', 'c', 'd', 'e');
console.log(xArray); // Prints: [ 'a', 'b', 'c', 'd', 'e' ]
console.log(returnArr(xArray)); // Returns: [ 'a', 'b', 'c', 'd', 'e' ]
console.log(XArray.isArray(xArray)); // Returns: true
```

Let's build the solution for the array and then simply implement that for the OP's question.

---

## The Solution

### Solution 1 - `Universal Solution` for all Runtimes

We will modify the `console.log()` inside the constructor function by modifying global object that has the `log` method.

> P.S. - You can `console.log(console)` to see all the console methods.

```
class XArray {
constructor() {
Object.defineProperties(this, {
length: {
writable: true,
enumerable: false,
configurable: false,
value: 0,
},
});

const runtimeConsole = console;
console = {
...console,
log: function (data) {
if (XArray.isArray(data)) runtimeConsole.log(Object.values(data));
else runtimeConsole.log(data);
},
};
}

push(...elements) {
for (const element of elements) {
this[this.length] = element;
this.length++;
}
}

static isArray(array) {
return array instanceof XArray;
}
}
```

---

### Solution 2: For `Node.js` using `util` module

We will be using the [`util.inspect.custom`][1] Symbol from the `util` module in `node.js`.

```
import util from 'util';

class XArray {
constructor() {
Object.defineProperties(this, {
length: {
writable: true,
enumerable: false,
configurable: false,
value: 0,
},
});
}

push(...elements) {
for (const element of elements) {
this[this.length] = element;
this.length++;
}
}

[util.inspect.custom]() {
return Object.values(this);
}
}
```

---

### Solution 3: Solving the OP's Use Case

So you can use either Solution 1 or Solution 2 to solve your Use Case:

#### Solution 3A: `Universal Solution` Using Solution 1

```es6
class User {
constructor(name, password) {
this.name = name;
this.password = password;

const runtimeConsole = console;
console = {
...console,
log: function (data) {
if (User.isUser(data)) runtimeConsole.log(data.name);
else runtimeConsole.log(data);
},
};
}

static isUser(user) {
return user instanceof User;
}
}
```

---

#### Solution 3B: For `Node.js` Using Solution 2

```es6
import util from 'util';

function User(name, password) {
this.name = name;
this.password = password;

[util.inspect.custom]() {
return this.name;
}
}
```

#### Testing Solution 3A and 3B:

```es6
// Test function that takes an user and returns it
const user = new User('example', 'password');

// Function that takes an user and returns it
const returnUser = user => user;

console.log(user); // Prints: example
console.log(returnUser(user)); // Prints: example
```

> **Sidenote**:
> - `import` is an ES6 convention that will not work in Node.js by default unless you have the `package.json` file have a `"type": "module"` setting enabled. This is because Node by default honours the CommonJS convention.
> If this is confusing, replace the import line and use:
> `const util = require('util');`


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through