12 ms·
Destructuring Assignment in ECMAScript 6
- kevincennis 13y agoFor anyone interested in trying some of this stuff out (which I highly recommend, because a lot of the new stuff in ES6 is really great), Google has an awesome tool called Traceur [https://github.com/google/traceur-compiler https://github.com/google/traceur-compiler] that will compile ES6 into ES5. There's also an online version here: [https://github.com/google/traceur-compiler https://github.com/google/traceur-compiler] that's great for quick little experiments. And, as an added bonus, it encodes your script in the URL so you can share it.
- thurn 13y agoSounds like the Modules implementation isn't ready yet, that's too bad... probably one of the most useful parts of ES6
- kevincennis 13y agoYeah. There are some other things that are actually impossible, like WeakMaps. But still, the stuff that exists is pretty awesome.
- santialbo 13y agoFor anyone wondering, TypeScript will also include support for this http://typescript.codeplex.com/workitem/15 http://typescript.codeplex.com/workitem/15
- mrspeaker 13y agoThis, and the short function syntax (that's also already in Firefox stable: "let square = x => x * x") make me very giddy. It's always been possible to write JavaScript in a functional style - but this kind of sugar makes it much much nicer. Things like Traceur will even let us transpile to old-school JS for old browser support, so for a lot of projects we can start using these things real soon.
- mnemonik 13y agoCombine the two: const tail = [, ...xs] => xs;
- Fistandantilus 13y agoIs Javascript's const head-const or tail-const?
- Benvie 13y agoconst tail = ([, ...xs]) => xs;
- mnemonik 13y agoAh you can't do the single-param-drop-parens when combined with destructuring? Good to know, thanks Benvie.
- Spoom 13y agoPHP has this available as list[1] in a slightly more limited form. 1. http://php.net/list http://php.net/list
- deleted 13y ago[deleted]
- agumonkey 13y agoI think you found PHP's third good part. -> https://jeena.net/php-the-good-parts https://jeena.net/php-the-good-parts
- andyroid 13y agoThat was my first thought too. I never really cared for list() in PHP as I find it a somewhat confusing way of assigning variables, and judging by the examples given in the article, I actually preferred the clarity of the less "terse" variable assignments done the old way. Might just be habit though.
- Spoom 13y agoYeah, I never really got it either. Seemed "clever", and I usually try to avoid being too "clever" with code. Readability almost always wins.
- agentultra 13y agoI can see why Brendan Eich is excited to get ES6 out there. It's finally coming back around to parity with Scheme. I'm looking forward to seeing more.
- AlexanderDhoore 13y agoJavascript is the functional programming community's secret weapon! Sure ES6 will add classes, but they're just syntactic sugar. Wrappers around the truly functional language underneath. Everyone is slowly being soothed into using a lisp. And when you realise this, it's the funniest thing to watch!
- agentultra 13y agoI'd be hard-pressed to call Javascript a "functional programming language" (whatever that means). Common Lisp also suffers from the same misguided label. In truth we love to mutate state in CL and find lattices of type hierarchies to be burdensome (but a great pool of research for your post-doc). Scheme is about as close as I'll get to pure-FP languages. I don't seem ES6 going down the "purity," route. And I hope it stays far from the border. What I was originally referring to though is (if I recall the history of the language) the roots of Javascript -- that it purportedly was a Scheme-like language before becoming what it is today. And by being what it is now has lagged behind in feature-parity to what programmers are used to in other languages whose history isn't tied to the venerable web browser. It just seems to me from the interviews that skirt the topic that Mr. Eich is finally coming back around to introduce what should have been there from the start. (Updated for clarity)
- AlexanderDhoore 13y agoOf course, it's not Haskell... I'm very aware that Javascript was inspired by Scheme. The lexical scoping for example, is obviously "functional" (indeed, whatever that means). Now they are introducing ways to more easily work with lists. Only thing missing is tail call optimization!
- 13y ago
- scribu 13y agoYet another great feature heavily inspired from CoffeeScript (and tastefully expanded upon). Well done! The surprising thing for me is that destructuring _almost_ allows defining default values for function arguments.
- mnemonik 13y agoDefault parameter values are also part of ES6.
- bsimpson 13y agoDefault parameter values, ...rest expressions, and modules were all part of ActionScript 3, which was an early implementation of JavaScript Next (then called ECMAScript 4). As I recall, Microsoft and Yahoo! threw a fit about it, and most of the interesting enhancements were shelved. Glad to see some of the good bits making their way back in. The number of times I've had to do function(firstParam, secondParam, thirdParam) { if (firstParam.hasOwnProperty('firstParam') { var argumentDict = firstParam; firstParam = argumentDict['firstParam']; secondParam = argumentDict['secondParam']; thirdParam = argumentDict['thirdParam']; } firstParam = firstParam || "default value"; ... is sickening.
- eurleif 13y agoWhy did Yahoo! care? Why did Yahoo! even have a seat at that table?
- ax 13y agoYahoo did and does have a spot at the table because they are a dues-paying member of ECMA. They have as much weight in the committee as any other member (MS, Google, Mozilla, now Facebook, etc.) Presumably Yahoo cares because they have millions of users running JS code.
- 13y ago
- cldr 13y agoGood to see, this is one thing I have really missed from Erlang when writing Javascript.
- mnemonik 13y agoThis isn't quite as powerful as full blown pattern matching (which in turn isn't quite as powerful as full blown unification) because you can't branch logic based on whether a pattern matches or not.
- cldr 13y agoDo you happen to know, which is `destructuring-bind`, destructuring or pattern matching?
- kibwen 13y agoCan you elaborate on what "unification" is in this context? The only language that I've used with full-blown pattern matching is Rust, and I'm curious to know what the next step up the ladder is.
- dragonwriter 13y agoA fairly decent, brief answer is here: http://stackoverflow.com/questions/4442314/differences-between-pattern-matching-and-unification http://stackoverflow.com/questions/4442314/differences-betwe...
- xhrpost 13y agoDefinitely looking forward to this as well as other new ES6 stuff. There's something else I feel I could use perhaps even more but I haven't seen it in any of the ES* updates. I'm a huge advocate of DRY and it bothers me when I have to code something like: var a = objImUsing.prop.someLongName > 5 ? objImUsing.prop.someLongName : whatever; I can and sometimes just make an extra variable above for a long prop list like this but that adds an extra line. I guess what I would like would be some sort of implied line limited scope: var a = objImUsing.prop.someLongName > 5 ? _1 : whatever; _1 referring to the first mentioned value in the immediate parent statement.
- ptgloden 13y agoIn ES6, you can write more terse immediately invoked function expressions. So you could use var a = (prop => prop > 5 ? prop : whatever)(objImUsing.prop.someLongName) rather than var a = (function (prop) { return prop > 5 ? prop : whatever })(objImUsing.prop.someLongName)
- mook 13y agoIn older versions of Firefox, you could write: var a = #1=objImUsing.prop.someLongName > 5 ? #1# : whatever; but it's been killed since Firefox 12: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Sharp_variables_in_JavaScript https://developer.mozilla.org/en-US/docs/Web/JavaScript/Shar...
- marshray 13y agoWho needs ML when now we have Javascript? I never thought I'd hear myself say that. But seriously, this is awesome. Thank you, whomever!
- crazygringo 13y agoThat's awesome. Any bets on when we can start using it on the front-end? I'm sure Chrome/FF will pick it up quick once it's finalized, but when will ECMAScript 6 be present in 95% of IE installations? 2020?
- pjmlp 13y agoYou are forgetting all the mobile browsers out there.
- kclay 13y agoThis is nice, reminds me of Scala
- STRML 13y agoThis is great and allows us to do some slicker functional code: function take(num, list) { if (num <= 0 || !list || list === []) return []; var [head, ...tail] = list; return [head].concat(take(num - 1, tail)); } It's not as elegant as haskell but it's still nicer than it would be without the new syntax. Once we have support for tail call optimization, things will get really interesting.
- vjeux 13y agoNote: list === [] doesn't do what you expect it to do. It's going to return false all the time because it is comparing the actual objects and not their content.
- sebastianmck 13y agoUse `!list.length` instead.
- STRML 13y agoAh, yeah, of course. That always threw me off, I've been working too much in other languages.
- NKCSS 13y agoWith the Destructuring Objects, I was like: WTF, but it all makes sense once you get to the Practical Applications of Destructuring. Nice stuff.
- masklinn 13y agoFWIW "objects" destructuring is just about as old as "tuple" destructuring: ML had both in the 70s, which it called "tuple patterns" and "record patterns".