镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

console-display: Symbol.toStringTag NOT work when extends EventTarget #37329

Description

@navegador5
  • Version: v16.0.0-pre
  • Platform: linux
  • Subsystem: ubuntu

What steps will reproduce the bug?

     //worked, no extends
     class ET  {
        #tag = "empty"
        constructor(tag) {
              this.#tag = tag
        }
        get [Symbol.toStringTag]() {return(this.#tag)}
     } 
     
     > var et = new ET("uuid-xxxx")
     > et
     ET [uuid-xxxx] {}                     //I want this effect 
     >
     
     
     
     //not work , when extends 
     class ETWithSig extends EventTarget {
        #tag = "empty"
        constructor(tag) {
              super();
              this.#tag = tag
        }
        get [Symbol.toStringTag]() {return(this.#tag)}
     }
     
     
     > var xt = new ETWithSig("uuid-yyyy")
     > xt
     ETWithSig {}                          //not-work when extends 
     >

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

Activity

  1. added
    utilIssues and PRs related to the built-in util module.
    on Feb 12, 2021
  2. targos commented on Feb 12, 2021

    @targos
    Member

    @nodejs/util

  3. self-assigned this
    on Feb 12, 2021
  4. BridgeAR commented on Feb 12, 2021

    @BridgeAR
    Member

    It's an issue with the custom inspect function defined for the EventTarget. These rarely mimic the regular inspect behavior.

  5. added
    eventtargetIssues and PRs related to the EventTarget implementation.
    and removed
    utilIssues and PRs related to the built-in util module.
    on Feb 12, 2021
  6. BridgeAR commented on Feb 12, 2021

    @BridgeAR
    Member

    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);
       }
     }
  7. benjamingr commented on Feb 12, 2021

    @benjamingr
    Member

    Just wondering - what are you doing with EventTarget?

  8. navegador5 commented on Feb 13, 2021

    @navegador5
    Author

    @benjamingr
    in my application:

    1. I need to make the each AST-NODE (from babel parse result) is a event-target,
    2. and a unique-tag is assigned(for log-collection to log-database-server) .
    3. using Symbol.toStringTag is just for console-manual-debug .

    It is a automation testbed for javascripts.

  9. removed their assignment
    on Mar 19, 2021
  10. github-actions commented on Jun 27, 2026

    @github-actions
    Contributor

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

  11. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jun 27, 2026
  12. github-actions commented on Jul 28, 2026

    @github-actions
    Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    eventtargetIssues and PRs related to the EventTarget implementation.staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions