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

Object.assign #221

Open
nelsonic opened this issue Sep 22, 2016 · 4 comments
Open

Object.assign #221

nelsonic opened this issue Sep 22, 2016 · 4 comments

Comments

@nelsonic
Copy link
Member

nelsonic commented Sep 22, 2016

Object.assign is an _ES6_ feature I really like because it is elegant.

// we have 3 objects with various properties:
var user = { name: 'Jack', email: '[email protected]' };
var meta = { height: '185', eyes: 'blue', hair: 'blonde' };
var social = { twitter: 'jackyboy', instagram: 'iamjack' };
// and combine them into a _new_ object with Object.assign:
var jack = Object.assign({}, user, meta, social);

This feels like the "right" way of combining Objects ... (why wasn't it always this way!?!?)

Object.assign works in Node.js (V8/Chrome), but sadly, is _not available_ (and will never be available) in _Internet Explorer_:
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Browser_compatibility
object assign-browser-compat

The ES5 version takes approx 20 seconds more to type and requires an extra line of code:

var jack = {};
[user, meta, social].forEach(function(o) { Object.keys(o).forEach(function(k) { jack[k] = o[k]; }); });

Try it: https://repl.it/DfSg/3 (can you simplify it?)

In order to write code that _Works Everywhere All-the-TimeTM_ is it time to investigate using Closure Compiler dwyl/learn-closure-compiler#1 so we can use ES6? or can people live with writing a few extra lines so we don't have to "transpile"...?

@jrans
Copy link
Member

jrans commented Sep 22, 2016

hehe @nelsonic. Yes will have to look into asap

@des-des
Copy link
Member

des-des commented Sep 22, 2016

Without mutatation (dont do this)

const pair2Str = ([k, v]) => "\"" + k + "\":" + JSON.stringify(v);
const entrySeq = o => Object.keys(o).map(k => ([k, o[k]]));
const entrySeq2JSON = entrySeq => '{' + entrySeq.map(pair2Str).join(',') + '}'
const merge = objArr => JSON.parse(entrySeq2JSON(objArr
  .reduce((wholeEntrySeq, obj) => wholeEntrySeq.concat(entrySeq(obj)), [])))
console.log(merge([{a: 1}, {b: 2}]))

@jackmoore
Copy link

I can't imagine not transpiling at this point, the ES6 features are such a big quality of life improvement. I fell in love so fast with Object.assign, then dumped it for the spread operator (using Babel, I'm not sure if Closure Compiler supports that).

@nelsonic
Copy link
Member Author

nelsonic commented Sep 24, 2016

@jackmoore yeah, agree Object.assign feels logical and is (way) more beginner friendly than the ES5 alternative... As for the spread operator from experience we have seen beginners struggle with them ...

maybe people who don't grok the spread operator "aren't meant to be programmers" ... but that's a debate for another place/time. our focus here is Object.assign

Also, forcing people to download 70MB of Babel ( 200MB on disk ) in each project (because there's no way I'm installing such a moving target of a dependency globally) and making our build/test process much slower thus eliminating the "productivity gains" achieved from typing fewer characters is not really an option ...

nelsonic added a commit to dwyl/learn-closure-compiler that referenced this issue Sep 24, 2016
@eliasmalik eliasmalik mentioned this issue Nov 3, 2016
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

7 participants