Skip to content

Isn't section 5.1 "Use object destructuring" conflicting with 3.3 "Use object method shorthand" ? #3136

Open
@ericmorand

Description

@ericmorand

In section 5.1 it is stated the following:

Use object destructuring when accessing and using multiple properties of an object.

In section 3.3, it is stated the following:

Use object method shorthand.

Let's look at the following example:

const foo = {
   bar: 5,
   // applying 3.3
   computeSomething() {
      return this.bar + 5;
   }
};

// applying 5.1
const {bar, computeSomething} = foo;

console.log(bar);

computeSomething();

The last instruction throws the following error:

TypeError: Cannot read properties of undefined (reading 'bar')

So, applying both 3.3 and 5.1 can lead to an incorrect code. Either we have to use arrow functions in objects definition; or we have to not use destructuring; but both 3.3 and 5.1 can't be recommended together without creating potential issues. At least, that's what I'm feeling.

What do you think? Did I miss the point or a clarification in the specs about that?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions