Destructuring Assignment in ECMAScript 6
fitzgeraldnick.com
fitzgeraldnick.com
There's also an online version here: [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.
const tail = [, ...xs] => xs; 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.
The surprising thing for me is that destructuring _almost_ allows defining default values for function arguments.
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.1) https://mail.mozilla.org/pipermail/es-discuss/2008-August/00...
(Disclaimer: I work for MS).
Javascript 1.7 was introduced in Firefox 2, in October 2006.
> I'd bet it wasn't a novel invention back then.
Indeed, ML featured destructuring assignment (which it called "patterns") of tuples and records back in the 70s.
"CoffeeScript programmers"? Is that a thing?
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. var a = (prop => prop > 5 ? prop : whatever)(objImUsing.prop.someLongName)
rather than var a = (function (prop) { return prop > 5 ? prop : whatever })(objImUsing.prop.someLongName)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)
No it wasn't. That's an urban legend. Eich WANTED to make a Scheme for the browser -- he didn't get a permission to do it, and made Javascript instead, which is actually inspired from other languages like Self.
Nothing functional about Javascript's horribly broken scoping either...
http://journal.stuffwithstuff.com/2013/07/18/javascript-isnt...
const myArray = [42];
myArray.push("Oh noes");
[42, "Oh noes"]EDIT: checked in Node, yep. That's unfortunate. Can we get a let foo immutable = [1]; or something?
EDIT2: Although this can be implemented in user-land with Object.freeze. Create a function that returns a const that is frozen if it is non-primitive. That should cover it, right?
There's not some secret functional language waiting to get out, any more that it is in any other language with first class functions, from Ruby to C#.
http://journal.stuffwithstuff.com/2013/07/18/javascript-isnt...
Language is more than syntax and set of features. How most people use it is very important, because you will have to interface with others people code, and your code will be interfaced with.
Javascript discourages OO programming because of pathological object creation syntax and unusual inheritance model. In javascript APIs you usually pass literals of a few common types and functions. In C# you pass instances of custom classes. The former is more suited for functional programming than the latter. In javascript you use closures for encapsulation, in C# you use private members.
I don't think I've ever seen "XManager" class in javascript :)
http://journal.stuffwithstuff.com/2013/07/18/javascript-isnt...
http://stackoverflow.com/questions/4442314/differences-betwe...
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?
I never thought I'd hear myself say that. But seriously, this is awesome. Thank you, whomever!