-
-
Notifications
You must be signed in to change notification settings - Fork 35.6k
console.log calls inspect function on passed objectΒ #8071
Copy link
Copy link
Closed
Labels
consoleIssues and PRs related to the console subsystem.Issues and PRs related to the console subsystem.docIssues and PRs related to the documentations.Issues and PRs related to the documentations.utilIssues and PRs related to the built-in util module.Issues and PRs related to the built-in util module.
Metadata
Metadata
Assignees
Labels
consoleIssues and PRs related to the console subsystem.Issues and PRs related to the console subsystem.docIssues and PRs related to the documentations.Issues and PRs related to the documentations.utilIssues and PRs related to the built-in util module.Issues and PRs related to the built-in util module.
Type
Fields
Give feedbackNo fields configured for issues without a type.
Version: v6.3.1
Platform: any
Code...
...will print:
That's because
console.logis using under the hood util.inspect which apparently will callinspectmethod 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
inspectmethod 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
toStringoverinspect?So this code...
...will print
fooinstead of123.Then at least I'll be able to define
toStringmethod and avoid nasty error for the users of my library.