Repository navigation
console-display: Symbol.toStringTag NOT work when extends EventTarget #37329
Description
Activity
- addedutilIssues and PRs related to the built-in util module.Issues and PRs related to the built-in util module.
on Feb 12, 2021 @nodejs/util
It's an issue with the custom inspect function defined for the EventTarget. These rarely mimic the regular inspect behavior.
- addedeventtargetIssues and PRs related to the EventTarget implementation.Issues and PRs related to the EventTarget implementation.and removedutilIssues and PRs related to the built-in util module.Issues and PRs related to the built-in util module.
on Feb 12, 2021 I put together a "fix" but we have to be aware that it's more like a bandage. There could be way more things that do not work as expected due to not being the actual object inspected.
Anyone may feel free to open a PR with this change.
diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index fe588088cc..92eb22f8cd 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -8,6 +8,9 @@ const { FunctionPrototypeCall, NumberIsInteger, ObjectAssign, + ObjectCreate, + ObjectSetPrototypeOf, + ObjectGetPrototypeOf, ObjectDefineProperties, ObjectDefineProperty, ObjectGetOwnPropertyDescriptor, @@ -32,7 +35,11 @@ const { } = require('internal/errors'); const { validateObject, validateString } = require('internal/validators'); -const { customInspectSymbol } = require('internal/util'); +const { + getConstructorOf, + customInspectSymbol +} = require('internal/util'); + const { inspect } = require('util'); const kIsEventTarget = SymbolFor('nodejs.event_target'); @@ -109,15 +116,14 @@ class Event { } [customInspectSymbol](depth, options) { - const name = this.constructor.name; if (depth < 0) - return name; + return this; const opts = ObjectAssign({}, options, { depth: NumberIsInteger(options.depth) ? options.depth - 1 : options.depth }); - return `${name} ${inspect({ + return `${this.constructor.name} ${inspect({ type: this[kType], defaultPrevented: this[kDefaultPrevented], cancelable: this[kCancelable], @@ -435,16 +441,30 @@ class EventTarget { [kCreateEvent](nodeValue, type) { return new NodeCustomEvent(type, { detail: nodeValue }); } + [customInspectSymbol](depth, options) { - const name = this.constructor.name; if (depth < 0) - return name; + return this; - const opts = ObjectAssign({}, options, { + const opts = { + ...options, depth: NumberIsInteger(options.depth) ? options.depth - 1 : options.depth - }); + }; + + const constructor = getConstructorOf(this) || EventTarget; + const object = ObjectCreate({ constructor }); + ObjectSetPrototypeOf(object, ObjectGetPrototypeOf(this)); + ObjectDefineProperty( + object, + SymbolToStringTag, + { + value: this[SymbolToStringTag], + enumerable: false, + writable: true + } + ); - return `${name} ${inspect({}, opts)}`; + return inspect(object, opts); } }
Just wondering - what are you doing with EventTarget?
@benjamingr
in my application:- I need to make the each AST-NODE (from babel parse result) is a event-target,
- and a unique-tag is assigned(for log-collection to log-database-server) .
- using Symbol.toStringTag is just for console-manual-debug .
It is a automation testbed for javascripts.
Reacted by Benjamin Gruenbaumgithub-actions commented
on Jun 27, 2026 on Jun 27, 2026 – with GitHub ActionsContributorMore actionsThis issue has been marked as stale due to 210 days of inactivity.
It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.- addedstaleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.Issues and PRs marked stale due to inactivity and scheduled for automatic closure.
on Jun 27, 2026 github-actions commented
on Jul 28, 2026 on Jul 28, 2026 – with GitHub ActionsContributorMore actionsThis issue has been automatically closed after 30 days of inactivity following its stale status (no activity for a total of 120 days).
If this is still relevant, feel free to reopen it or leave a comment with additional details so we can continue the discussion.
What steps will reproduce the bug?
How often does it reproduce? Is there a required condition?
always
What is the expected behavior?
see the reproduce worked-example
What do you see instead?
see the reproduce extends-example