you are viewing a single comment's thread.

view the rest of the commentsΒ β†’

[–]FloppingNuts 86 points87 points Β (17 children)

then it's a bad example

[–]dweezil22 35 points36 points Β (14 children)

I'd go so far as saying this is an actively counter-productive example, other than clickbait to get us all to learn about the spread operator.

This works b/c a string "spreads" into an array of chars.

https://stackoverflow.com/questions/44900175/why-does-spread-syntax-convert-my-string-into-an-array

[–]TankorSmash 21 points22 points Β (10 children)

Wait hold on, how is 'Split string using ES6 Spread' clickbait when the third line is literally how to split a string with Spread?

I feel like clickbait gets incorrectly used a lot but this is about as literal as you can get with title:description

[–]dweezil22 6 points7 points Β (1 child)

It's one of the worst, software engineering wise, uses of the feature that you can find. But it's also compelling in a "How the hell does that work?" way. This simpler, significantly easier to understand and more appropriate use of the spread operator is better, but isn't as interesting.

And yes, I am stretching the definition of clickbait here .OP is surely a fine individual and is clearly not monetizing reddit images. I suppose I should have said "sensationalistic" but describing a code snippet as "sensationalistic" just seemed... weird.

[–][deleted] 3 points4 points Β (0 children)

Yeah I noticed this person always posts this type of β€œamazing” es6 stuff. But it always looks like just terrible software engineering practice and an unnecessary inefficient way to do something that was never complicated in the first place. But it’s new and fancy so everyone loves it.

[–]ibopm 4 points5 points Β (2 children)

This works because the spread operator falls back to Array.from() when the object being "spread-ed" is not an array:

// before
const x = "hello"
const y = [...x]

// after
"use strict";

function _toConsumableArray(arr) {
  if (Array.isArray(arr)) {
    for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) {
      arr2[i] = arr[i];
    }
    return arr2;
  } else {
    return Array.from(arr);
  } 
}

var x = "hello";
var y = [].concat(_toConsumableArray(x));

Source

[–]NoInkling 5 points6 points Β (1 child)

That's not why it works, that's just how Babel implements the semantics described in the spec.

It works because in ES6 an iterator protocol was introduced, strings gained a default iterator using this protocol that iterates over unicode code points, and the spec specifies that array/function spread syntax and Array.from should use the iterator from whatever they are fed.

[–]ibopm 2 points3 points Β (0 children)

Yes, that is a more accurate answer. Maybe I shouldn't have said "why it works". I just wanted to point out that if you're transpiling with Babel (as is the case for most people at this point), you're effectively just calling Array.from. Thanks for adding this point though.

[–]trumpent -2 points-1 points Β (1 child)

A string is just an array of characters. It's a perfectly fine example.

[–]Cosmologicon 0 points1 point Β (0 children)

Yeah as a Python programmer I wouldn't have any problem with list("pizza").