56497

Should I declare a mutable array/object as a const in ES2015? [closed]

If it was an immutable object, then I would have no doubt in declaring it as a const. But in this case it's just a const reference to an object while the object is modified later in the code.

const styles = {}; ... styles.width = rect.width + 'px';

I'm afraid it will be confusing for developers reading this code. It feels like let is more appropriate for objects that get mutated.

Answer1:

This is probably a question you wont be able to get a final answer for, as it is very opinion-based.

A little bit of historical background can help to understand how the confusing const came to be in the first place:

Allan Wirfs-Brock, the former editor of the EcmaScript spec, himself said:

In a slightly different (and perhaps better) universe, ECMAScript const declaration would be named let and introduce a SSA binding.

And asked, why this was not the case, he responded:

let/const was a carry over from ES4 proposals and Moz JS1.7

TC39 never seriously explored naming alternatives

So you could say const is a bit of a misnomer, since it doesn't do what people who aren't familiar with it might expect it to do.

Since const works the way it does though and is almost certainly not going to change, <strong>I would still define your objects with const</strong>.

This might help people who are unfamiliar with the way const works to get a better understanding.

Maybe it could help to add a comment at the first occurrence of const with an object, along the lines of:

// Important: 'const' only ensures that 'styles' always references the same object, // it does not render it immutable const styles = {};

Opinions may vary though.

Recommend

  • How can I enforce second-degree relationships without composite keys?
  • How to re run dom-repeat with sort when bool property changed in Polymer element
  • Vue closing component returns avoid mutating a prop directly
  • Why “fopen” function does not use enumerations?
  • Can I use twitter api to search tweets one week before?
  • JQuery not working in Ajax load page
  • Swift 1.2 assigning let after initialization
  • What's the benefit of the trailing apostrophe in character literals
  • ViewData, ViewBag and TempData violates MVC? [closed]
  • jQuery and Uploadify session in the php file
  • Where I store the custom exceptions in cakephp 3?
  • Android: how to determine cold start
  • How to add System.Windows dll to Visual Studio 2010 express?
  • CoreData basics – to-many relationship array data
  • Query to find the duplicates between the name and number in table
  • presentShareDialogWithParams posts to FB wall, but callback handler results say error
  • custom UITableViewCell with image for highlighting
  • Content-Length header not returned from Pylons response
  • Use of this Javascript
  • Allowing both email and username for authentication
  • Google Custom Search with transparent background
  • Android fill_parent issue
  • C++ Partial template specialization - design simplification
  • how to adjust image in a panel in Java swing?
  • Ajax Loaded meta Tags
  • Get one-time binding to work for ng-if
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Xamarin Forms - UWP Fonts
  • Possible to stop flickering java tooltip in heavyweight mode?
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • How do you troubleshoot character encoding problems?
  • Jquery - Jquery Wysiwyg return html as a string
  • How to get next/previous record number?
  • Arrays break string types in Julia
  • Python: how to group similar lists together in a list of lists?
  • WPF Applying a trigger on binding failure
  • JTable with a ScrollPane misbehaving
  • Java static initializers and reflection
  • Django query for large number of relationships