News for hackers. Stuff that matters.
[dead]
Re:Cheatsheet for the modern JavaScript by derpydev on Sunday October 01, 2017 @09:33AM (#15377717)
You seem to be mixing up the concepts of "constant" and "immutable". In the context of JS, constant is a characteristic of the variable identifier and only suggests that it will never be reassigned to point to another value or reference in the current scope. The mutability of the referenced value is entirely up to the definition of said value's type (hence why stuff like Immutable.js exists).

The idea that JS programmers can't learn from others' languages first is an odd one considering that, if I remember correctly, const in JS has the same semantics as the final keyword in Java and it's the default semantics of any local variable in Rust, so it's not something that seems to be out of the ordinary or falling out of favor.

Re:Cheatsheet for the modern JavaScript by K0nserv on Sunday October 01, 2017 @06:54AM (#15377256)
But the values are const? You just need to understand the implication of const in javascript.

Why would you choose to weaken the communication in your code by choosing `let` over `const` when you have no intention of reassign the value? `let` would just confuse the reader as they'd start anticipating the reassignment.

Compare:

```

// A variable containing an object that will not be reassigned

const a = { foo: 10};

// This is still valid

a.foo = 10;

```

vs

```

// A variable containing an object that might be reassigned.

let a = { foo: 10};

// This is still valid

a.foo = 10;

```

The first example says more about your intent and throwing that information away is a bad idea.

Re:Cheatsheet for the modern JavaScript by mbrock on Sunday October 01, 2017 @01:52PM (#15378910)
Let is a much nicer keyword though. It just reads better.

I almost never use variable mutation in JS, and I use let almost everywhere.

That lets me use the let/const distinction in a way that to me seems even more informative, namely to distinguish actual constants like "site base URL" or "pi".

Re:Cheatsheet for the modern JavaScript by nerdponx on Sunday October 01, 2017 @09:33AM (#15377718)
I get your point, but I'm not sure I share your cynicism. For instance, a "constant" container with mutable contents is the first example given in Practical Common Lisp [0]:

    (defvar *db* nil)

    (defun add-record (cd) (push cd *db*))
    (defun make-cd (title artist rating ripped)
      (list :title title :artist artist :rating rating :ripped ripped))

    (add-record (make-cd "Roses" "Kathy Mattea" 7 t))
If you're concerned about this kind of thing, we should be encouraging novice programmers to stop using language features they don't understand.

[0]: http://www.gigamonkeys.com/book/practical-a-simple-database....

Re:Cheatsheet for the modern JavaScript by yorwba on Sunday October 01, 2017 @10:19AM (#15377948)
My Lisp is a bit rusty, but I think actual constants are written +with-pluses+, whereas * the-stars * indicate global variables. And that database isn't just internally mutable, sometimes it's replaced completely [0]:

  (defun load-db (filename)
    (with-open-file (in filename)
      (with-standard-io-syntax
        (setf *db* (read in)))))
[0]: http://www.gigamonkeys.com/book/practical-a-simple-database....
Re:Cheatsheet for the modern JavaScript by vectorpush on Sunday October 01, 2017 @01:44PM (#15378889)
It's unfortunate that you take such an inflammitory tone when you're so completely incorrect. They are constant, end of story.
Re:Cheatsheet for the modern JavaScript by benjaminsuch on Sunday October 01, 2017 @05:59AM (#15377115)
If the value of that "variable" is not supposed to be changed, I use const.
Re:Cheatsheet for the modern JavaScript by Fifer82 on Sunday October 01, 2017 @08:36AM (#15377567)
I also don't get this point (I am not the brightest bulb :D)

In Typescript anyway, `const` means I am not allowed to reassign. Attempting to change it just will cause compiler warnings. If you mean that, ignore Typescript and tooling, and that once the code is in the browser executing, then consts can be changed.... Who cares about that?