Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inspect reserved keyword for class function #10650

Closed
aabfred opened this issue Jan 6, 2017 · 4 comments
Closed

inspect reserved keyword for class function #10650

aabfred opened this issue Jan 6, 2017 · 4 comments
Labels
util Issues and PRs related to the built-in util module.

Comments

@aabfred
Copy link

aabfred commented Jan 6, 2017

  • Version: v7.3.0 (npm v3.10.10)
  • Platform: GNU/Linux Fedora release 25 (Twenty Five) 4.8.15-300.fc25.x86_64
class MyClass {
    constructor(){ ... }
    inspect(){
        ... some code for a function I create ...
    }
}
Promise.all([new MyClass()]).then(console.log)

=> [ Promise { < pending > } ] !!!

I've found the reason here: Custom inspection functions on Objects
What a bad idea !!! It's a real bug factory

To add such features without being intrusive, your syntax should look like:

class MyClass {
    constructor(){ ... }
    [Symbol.for("inspect")](depth, options){ ... }
}
@addaleax addaleax added the util Issues and PRs related to the built-in util module. label Jan 6, 2017
@addaleax
Copy link
Member

addaleax commented Jan 6, 2017

You’re totally right! See #8071 for some previous discussion on that. In short, we do support Symbol-based inspection now, and it can even be used to override the “inspect” name.

Unfortunately, getting rid of “inspect” as the name for the method is not too easy due to the required backwards compatibility – “inspect” is a lot older than Symbols in Javascript, so back then the choice made sense, and by now a lot of people have come to rely on that behaviour.

If you have any suggestions for improving the situation that are not breaking changes, that would be hugely appreciated.

@aabfred
Copy link
Author

aabfred commented Jan 6, 2017

You're right, sorry.
If you can, remove this item or tell me a way to do it.

@addaleax
Copy link
Member

If you can, remove this item or tell me a way to do it.

This item? I’m not sure what you mean.

In any case, adding a [util.inspect.custom]() { return this; } method to your class should solve your particular problem.

@jasnell
Copy link
Member

jasnell commented Jan 15, 2017

Assuming this can be closed now

@jasnell jasnell closed this as completed Jan 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
util Issues and PRs related to the built-in util module.
Projects
None yet
Development

No branches or pull requests

3 participants