News for hackers. Stuff that matters.
Re:What’s New in ES2019 by wongarsu on Tuesday July 30, 2019 @06:26AM (#20563500)
Mutation is a real weakness of Javascript. I think the general idea is "methods don't mutate unless they are ancient". For example Array.map (an IE9-era feature) doesn't mutate, Array.sort (an IE5.5-era feature) does. Similarly a "for(let i=0; i<arr.length; i++) arr[i] = b" loop will obviously mutate modified elements while a "for(let e of arr) e = b" won't; the trend is towards less mutability in new features.

Proper immutable support (or a stronger concept of const) would also help with this.

Re:What’s New in ES2019 by jacobwilliamroy on Tuesday July 30, 2019 @06:53AM (#20563638)
> for(let e of arr) e = b

Is that just

  arr.map( e => b )

?
Re:What’s New in ES2019 by wongarsu on Tuesday July 30, 2019 @08:04AM (#20564038)
It doesn't do anything and was my attempt to give a simple example that is somewhat obvious while glossing over the complexity

    let arr = [{a: 1, b: ["a", "b"]}, {a: 9, b: ["a","c"]}];
    let b = "!"

    for(let e of arr) e = b;
    console.log(arr)
[{a: 1, b: ["a", "b"]}, {a: 9, b: ["a","c"]}] (unmodified)

    for(let e of arr) e.a = 2;
    console.log(arr)
[{a: 2, b: ["a", "b"]}, {a: 2, b: ["a","c"]}] (modified)

    for(let e of arr) {let copy = {...e}; copy.a = 4;}
    console.log(arr)
    
[{a: 2, b: ["a", "b"]}, {a: 2, b: ["a","c"]}] (unmodified)

    for(let e of arr) {let copy = {...e}; copy.b[0] = "!";}
    console.log(arr)
    
[{a: 2, b: ["!", "b"]}, {a: 2, b: ["!","c"]}] (modified)

The most frustrating thing about all of this is that the best way to make a deep copy to avoid all unwanted modification is JSON.parse(JSON.stringify(arr))

Re:What’s New in ES2019 by jacobwilliamroy on Tuesday July 30, 2019 @09:21AM (#20564551)
Thanks for sharing more context. I see what you were trying to illustrate with your earlier examples now.

Why not make a little library so you can do these kinds of things safely without reimplementing them every project? Maybe call it safeCopy or something.

Re:What’s New in ES2019 by Izkata on Tuesday July 30, 2019 @08:22AM (#20564142)
...honestly, I don't see much complexity here. Understanding of reference types and the difference between deep copy and shallow copy makes it pretty straightforward - the result is the same as it would be in python or java.
Re:What’s New in ES2019 by wongarsu on Tuesday July 30, 2019 @08:48AM (#20564305)
I don't think Python or Java are examples of good or simple behavior here. Java at least has a strong culture of "copy everything or make it immutable at class boundaries" while javascript libraries often leave you guessing.

Examples where this is easy are C where every copy and allocation is very explicit, C++ which has a good `const` concept that's actually useful, or Rust with it's ownership concept that makes mutability very obvious.

Re:What’s New in ES2019 by nullandvoid on Tuesday July 30, 2019 @08:47AM (#20564296)
Providing It was syntactically correct the first call would assign e to a variable b for each iteration ( pretty pointless )

The arr.map providing you had a variable on the left side would output the result of each iteration into said array ( so an array containing all b values - however I guess you meant arr.map(e => e)

Re:What’s New in ES2019 by wongarsu on Tuesday July 30, 2019 @08:53AM (#20564333)
> Providing It was syntactically correct

Providing arr is iterable (e.g. an array) it's perfectly valid js. Your linter might scream at you to add braces and a semicolon, but neither is needed for correctness here.

Re:What’s New in ES2019 by jacobwilliamroy on Tuesday July 30, 2019 @09:10AM (#20564460)
Nope. I meant arr.map(e=>b) .

I try to reimplement pointless code, I get more pointless code.

Re:What’s New in ES2019 by amitport on Tuesday July 30, 2019 @07:32AM (#20563851)
Nope. The first does not do anything. The second makes a new array of length 'arr.length' with 'b' in every cell
Re:What’s New in ES2019 by dao- on Tuesday July 30, 2019 @07:21AM (#20563787)
No, it's broken code and doesn't effectively do anything.
Re:What’s New in ES2019 by davidjnelson on Tuesday July 30, 2019 @11:26AM (#20565740)
Typescript readonly properties and types are amazing for this. ReadonlyMap<T>, ReadonlyArray<T>, Readonly<T>.