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

console.log calls inspect function on passed object #8071

Closed
szwacz opened this issue Aug 11, 2016 · 5 comments
Closed

console.log calls inspect function on passed object #8071

szwacz opened this issue Aug 11, 2016 · 5 comments
Labels
console Issues and PRs related to the console subsystem. doc Issues and PRs related to the documentations. util Issues and PRs related to the built-in util module.

Comments

@szwacz
Copy link

szwacz commented Aug 11, 2016

Version: v6.3.1
Platform: any

Code...

console.log({
    inspect: function () {
        return 123;
    },
});

...will print:

123

That's because console.log is using under the hood util.inspect which apparently will call inspect method on given object if finds one.

This behaviour is very suprising and looks like a bug to someone who didn't read that particular doc. As a matter of fact I also maintain library which has inspect method as part of its API. So doing console.log(myLib) will lead to obscure error.

Solution?
Both APIs console and util have status stable so I believe there is no way to alter this behaviour.
But how about starting favouring toString over inspect?
So this code...

console.log({
    inspect: function () {
        return 123;
    },
    toString: function () {
        return 'foo';
    },
});

...will print foo instead of 123.

Then at least I'll be able to define toString method and avoid nasty error for the users of my library.

@addaleax addaleax added the console Issues and PRs related to the console subsystem. label Aug 11, 2016
@addaleax addaleax added the util Issues and PRs related to the built-in util module. label Aug 11, 2016
@addaleax
Copy link
Member

addaleax commented Aug 11, 2016

But how about starting favouring toString over inspect?

I am not sure that’s a good idea, both because inspect has been around for a long time and because they have different semantics; toString() is used when something machine-readable is needed, whereas .inspect() is for humans and can even return non-strings.

A Symbol property that would take precedence over .inspect might be a better idea to work around this because that’s guaranteed not to conflict with anything.

@jasnell
Copy link
Member

jasnell commented Aug 11, 2016

+1.. was just thinking the same thing. Having a util.Inspect symbol such that {[util.Inspect]:function() {}} is favored over {inspect:function(){}} would be good in theory. In practice, however, it may be quite difficult, and take some time, to get implementers to update to the new Symbol. It's not out of the question tho and would be a good best practice to encourage.

@silverwind
Copy link
Contributor

In 6.4.0, you should be able to disable this behaviour through:

util.inspect.defaultOptions.customInspect = false

@vkurchatkin vkurchatkin added the doc Issues and PRs related to the documentations. label Aug 16, 2016
@addaleax
Copy link
Member

See #8174 for a symbol-based approach that would make it possible to ignore the inspect property of an object.

addaleax added a commit to addaleax/node that referenced this issue Aug 24, 2016
Add a `util.inspect.custom` Symbol which can be used to customize
`util.inspect()` output. Providing `obj[util.inspect.custom]`
works like providing `obj.inspect`, except that the former allows
avoiding name clashes with other `inspect()` methods.

Fixes: nodejs#8071
addaleax added a commit to addaleax/node that referenced this issue Sep 7, 2016
Add a `util.inspect.custom` Symbol which can be used to customize
`util.inspect()` output. Providing `obj[util.inspect.custom]`
works like providing `obj.inspect`, except that the former allows
avoiding name clashes with other `inspect()` methods.

Fixes: nodejs#8071
PR-URL: nodejs#8174
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <mic.besace@gmail.com>
Fishrock123 pushed a commit to Fishrock123/node that referenced this issue Sep 8, 2016
Add a `util.inspect.custom` Symbol which can be used to customize
`util.inspect()` output. Providing `obj[util.inspect.custom]`
works like providing `obj.inspect`, except that the former allows
avoiding name clashes with other `inspect()` methods.

Fixes: nodejs#8071
PR-URL: nodejs#8174
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <mic.besace@gmail.com>

PR-URL: nodejs#8437
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Fishrock123 pushed a commit to Fishrock123/node that referenced this issue Sep 8, 2016
Add a `util.inspect.custom` Symbol which can be used to customize
`util.inspect()` output. Providing `obj[util.inspect.custom]`
works like providing `obj.inspect`, except that the former allows
avoiding name clashes with other `inspect()` methods.

Fixes: nodejs#8071
PR-URL: nodejs#8174
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <mic.besace@gmail.com>

Refs: nodejs#8437
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Fishrock123 pushed a commit that referenced this issue Sep 9, 2016
Add a `util.inspect.custom` Symbol which can be used to customize
`util.inspect()` output. Providing `obj[util.inspect.custom]`
works like providing `obj.inspect`, except that the former allows
avoiding name clashes with other `inspect()` methods.

Fixes: #8071
PR-URL: #8174
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <mic.besace@gmail.com>

Refs: #8437
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
addaleax added a commit to addaleax/node that referenced this issue Nov 18, 2016
Add a `util.inspect.custom` Symbol which can be used to customize
`util.inspect()` output. Providing `obj[util.inspect.custom]`
works like providing `obj.inspect`, except that the former allows
avoiding name clashes with other `inspect()` methods.

Fixes: nodejs#8071
PR-URL: nodejs#8174
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <mic.besace@gmail.com>
@aabfred
Copy link

aabfred commented Jan 6, 2017

We know a solution: use symbols but it cannot respect backward compatibility.
But the worst problem is communication on nodeJS reserved keywords that should be on the first doc page to avoid wastes of time while debugging.
Then, future solution should be available on newest versions to start using a right syntax.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
console Issues and PRs related to the console subsystem. doc Issues and PRs related to the documentations. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

No branches or pull requests

6 participants