question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Do not mutate objects

See original GitHub issue

I’m submitting a … (check one with “x”)

[x] bug report => search github for a similar issue or PR before submitting
[x] feature request

Current behavior The table mutate the objects / raws with an $$index property. code

Expected behavior Do not mutate objects.

What is the motivation / use case for changing the behavior? It is considered a bad practice to mutate objects sent by the user. With today’s modern language constructs it can be easily avoided.

The issues vary, here is a summery:

  1. Impossible to handle immutable objects
  2. Unintentional reflection of the $$index property
  3. Usage conflict (the user or other 3rd party lib’s might be using $$index

The user is not aware that this happens and this might create serious implications with the current implementation.

For example:

A user might use the ImmutableJS library or use Object.freeze/seal on the objects sent as rows, this will cause an exception by the library that can’t handle these objects. (ImmutableJS is a quite common library)

If an object is sent to the table and then used to create an HTTP request the $$index property will be sent along with the request. This is unintentional reflection.

There are several solution to solve this, this is my preferred solution:

  • Use WeakMap to store the index of every row, rows are automatically removed from the WeakMap instance when removed from the table (and no longer referenced by the user). Each table instance should have a WeakMap instance. It is possible to remove the row from the weekmap when removed from the table.
  // somewhere at construction: 
  this.idxMap = new WeakMap() 

  // set index:
  this.idxMap.set(row, rowIndex);


 // use this.idxMap.get to fetch the index.

This solution can’t create conflicts, it will not reflect on object serialization and it will not mutate the object.

Other possible solutions:
  • Use a Symbol instead of a property, symbol are guaranteed not to conflict with any other library.
  // somewhere at module's top: 
  IndexSymbol = Symbol('ngx-datatable RowIndex');

  // set:
  row[IndexSymbol] = rowIndex;

 // get
 row[IndexSymbol]

This solve any conflict and will not reflect in serialization but it will throw on immutable objects.

  • Assign $$index with a property descriptor so the value is not enumerable.
  Object.defineProperty(row, '$$index', { value: rowIndex });

This does not solve usage conflicts and mutates the object, not good.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
amcdnlcommented, Jul 10, 2017

Planning on this for next release, probably be in July sometime.

2reactions
amcdnlcommented, Aug 4, 2017

done in 10.0.0!!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Avoid Object Mutation in JavaScript | by Jake Mills
Three Quick Ways to Avoid Mutating Objects in JavaScript & Gratuitous Jon Stewart Gifs ... }let avenger = character;character.lastName = 'Marvel'; ...
Read more >
Washing your code: avoid mutation - Artem Sapegin
Mutations happen when we change a JavaScript object or array without creating a new variable or reassigning an existing one.
Read more >
Mutation isn't always bad in JavaScript - DEV Community ‍ ‍
The truth is mutation isn't always bad, nor is it usually bad. ... You don't need mutation to update a property of an...
Read more >
Object Mutation In JavaScript - Dev Genius
All primitives are immutable; that is, they cannot be altered. The variable may be reassigned to a new value, but the existing value...
Read more >
Immutability in React: Should you mutate objects?
Despite popular belief, there's actually nothing wrong with mutating objects. In certain situations, like concurrency, it can become a problem, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found