Skip to content

Problem when trying to reassign an object altered with the LS delete operator with the latter's return value #273

@ymeine

Description

@ymeine

The title was hard to find for me, to make it clear and concise, but actually the problem is simple.

Whether this is a good practice or not, here is what I've done:

During one of my developments, I tried to do something I often do which is recursively traversing a tree from the root to the leaves, removing some properties on each node.
Here the tree had a simple from:

  • the root
  • each node has only ONE child node, except the leaf of course

Simple right?

Now the problem comes from the fact that the property I wanted to remove on each node during the operation was the child node itself.

So here was my first implementation:

let node = root
    while node.child?
        node = delete node.child

Considering the documentation, I would expect it to work.

However, looking at the compiled JavaScript:

(function(node){
  while (node.child != null) {
    node = node.child, delete node.child;
  }
}.call(this, root));

we see that the problem comes from the order of the operations: the node variable is assigned before the property is deleted, but here we wanted to delete a property of node.

This has been done this way to avoid a temporary variable I guess.

In the case I shew, there is a simple workaround, using an implicit variable acting as the missing temporary variable: the that reference coming from the test.

let node = root
    while node.child?
        delete node.child
        node = that

However I think this is an issue, or otherwise change the documentation to be clear on this fact.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions