Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ellipsis spread syntax #230

Open
nelsonic opened this issue Sep 24, 2016 · 6 comments
Open

ellipsis spread syntax #230

nelsonic opened this issue Sep 24, 2016 · 6 comments

Comments

@nelsonic
Copy link
Member

The examples on MDN are kinda useless ...
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Spread_operator

We need to write a couple of real world examples of where using spread operator in a real-world scenario clarifies the code. (not a fibonacci calculator ... or other manufactured "reason" to use them, a scenario where it's obviously better...)

@jrans
Copy link
Member

jrans commented Sep 29, 2016

// jack wants to make some home made jam and butter and need an ingredient list
   var jam = { sugar: '1 lb', strawberries: 20 };
   var butter = { buttermilk: '1 lt', salt: 'pinch' };

  var ingredientList = { ...jam, ...butter }; 
// note assumption that jam and butter have different ingredients 

// maybe bad example just wanted to to spread some jam and butter!

@jrans
Copy link
Member

jrans commented Sep 29, 2016

   var youCanLookButDontTouch = { no: 'mutations', pretty: 'please', iam: 'shallow' };

   var quickShallowCopy = ...youCanLookButDontTouch;

@jrans
Copy link
Member

jrans commented Sep 29, 2016

var customConfig = { ...baseConfig, change: 'option' };

or

var customConfig = { default: 'value', another: 1, ...customOptions };

finally

var customConfig = { ...defaultOptions, ...customOptions }

All examples rely on of spread to make customisation on a base set of options.

@nelsonic
Copy link
Member Author

Are you hungry @jrans ? want me to make you a sandwich ...? 😉

So in this case you are doing an _ES7_ object spread/copy, which you could achieve using Object.assign() which I think is clearer to a beginner even thought it's more to type.
see example on Redux docs: http://redux.js.org/docs/recipes/UsingObjectSpreadOperator.html

I agree that it's "more verbose" ... but sometimes that makes code more human/beginner-friendly...

Spread is available for Array in ES2015 https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Spread_operator#Specifications but for Object it's only a proposal https://github.com/tc39/proposals at "Stage 2" (i.e "Draft") scheduled for inclusion in ES2017 ... should we be encouraging people to use Object Spread for assignment/destructuring i.e. forcing people to use a "transpiler"?

I tried using it with Google Closure Compiler and got an Parse error. see: see: google/closure-compiler#2027 (comment) (discussion of ES2017 features)

@jrans
Copy link
Member

jrans commented Sep 29, 2016

I'd argue that Object.Assign has the same learning curve as the ellipsis ... but of course confusion can keep in for beginners when confronted with destructuring with rest.

Certainly if problems compiling then Object.Assign is good enough so I think we can let this issue ...rest?

@nelsonic
Copy link
Member Author

@jrans happy to let it ...rest for now... 😉
(we can return to it when its a bit closer to being a standard and is available in Closure Compiler or what ever we decide to use...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants