22 Nov 2011
ECMAScript 6 looks promising
I am quite excited about ECMAScript 6, after watching David Herman’s talk at YUIConf 2011. I am especially looking forward to seeing some of these features landing up on V8 soon, so that I can use it on node.js. These additions will solve many common sources of frustration that newcomers face when working with JavaScript. Although the spec is not expected to be finalized till 2013 (so says David in the video), a lot of these features are expected to hit Chrome and Firefox much before that.
I am personally looking forward to the following:
let
keyword
The let
keyword has a block scope. Since var
has a function scope, it sometimes leads to unintended bugs. Going forward, we can completely replace usages of var
with let
to avoid such bugs.
Default arguments
function foo(bar="baz") { console.log(bar); } |
With default arguments, we don’t need the convoluted options object pattern.
Non-strict destructuring
Looks Pythonic, except that it’s not as strict.
let [x,y] = [3,4,5]; // x=3, y = 4 |
Multi-line strings
You can simply use the `
(backtick) operator to denote multi-line strings.
var htmlString = `Say hello to multi-line strings!` |
Templating
You can also embed JavaScript variables amidst your strings this way:
var firstName = "Jack"; var message = `Hello ${firstName}!`; // "Hello Jack!" |
List comprehension
Again, a very pythonic construct:
let even = [ x for (x in values([1,2,3,4,5,6])) if (x %2 === 0) ]; |
The use of values()
allows x
to denote the element values, rather than the element indices. This could also be written using the new for of
construct:
let even = [ x for(x of [1,2,3,4,5,6]) if (x%2 === 0) ]; |
Apart from these, map
, filter
, reduce
and so on will be part of the gang (some of these are already implemented in Chrome, Firefox, and (gasp) IE9).
UPDATE:
Noticing that this post has hit the frontpage on HN, I must add that I left out the proposed module system. Examples from the talk:
import { $ } from "jquery.js" import { map, each } from "underscore.js" |
Once again, it’s all Python! The referred JavaScript files will actually be loaded by your browser before the code is executed.
You can follow me on Twitter right here.
You mispelled firstname in the Templating example
@Joshua: Thanks, I fixed the typo.
Thanks for the overview.
I must say I don’t care for “let” for silly reasons (i.e. “let” is a throwaway keyword from BASIC—let foo = bar === foo = bar).
Default arguments are nice, but they don’t replace the options argument pattern which has the virtue of allowing only the values you want in the order you want.
I’m sure JavaScript grabbing ideas from Python gives Pythonistas are happy glow, but as long as it doesn’t take indentation from Python they can have their glow.
comment = ‘’’
Oh no!
Backquoted multi-line strings! Whose bad idea was that?
Can we at least identify the person that proposed it, so we can execute him to set an example?
‘’’
This doesn’t bode well for the other ideas. Why patch up Javascript and not reboot it? That would have practically zero costs in the VM.
Rebooting Javascript means deprecating all the bad parts and build forward from the good parts. Give it another MIME type and we’re done.
Stop dragging along the bad parts. It is so x86.. (case in point: in a multi-core processor 32b legacy mode would be useful only for the main core, not the others).
Let as a keyword did not start with BASIC. Go check your LISP history.
@gart: Google is probably going to attempt that via Dart. Though, I’m somehow not at all excited by Dart. I don’t see a reboot happening when it takes a few years just to get the various browser vendors to improve an existing language.
@Sean And actually this meaning is closer to the way that was created for LISP than BASIC
Import….wow. because simply using the commonjs spec would be way too easy.
These proposals just change the syntax really. Something like coffeescript can (or does) take care of that, without compromising compatibility. I’d really love to see more FP ideas make it to js. Efficient immutable versions of collection literals (e.g: #[1,2,3] and #{a:1,b:2,c:3}) and tail-call optimisation would greatly simplify things for me. Set literals would be handy too, with a syntax like: {1,2,3} for mutable and #{1,2,3} for immutable.
@jcd0589: how would you load modules ahead of time with CommonJS ?
See http://t.co/7kjPszW5
let1 has actually been in Firefox for quite a while as a JavaScript 1.7 feature.
[1] https://developer.mozilla.org/en/JavaScript/Reference/Statements/let
JS is a mess. But what’s worse is the ‘memory leaks’, especially with nodejs apps. The industry is going down yet another rabbit hole.
[...] 本文从ECMAScript 6 looks promising翻译而来。 [...]
Is that really templating or just string interpolation? ( http://en.wikipedia.org/wiki/String_interpolation )
I realize that there is a strong religion against operator overloading, and indeed understand why it can cause so much trouble. BUT there is one place where it is just wonderful: mathematics.
Reading a complex equation that uses function notation for advanced mathematical objects like vectors, matrices and so on is just very unreadable compared to the notation of, for example Octave/Matlab.
So I am discouraged with the lack of overloading as part of the conversation on “whither JS”.
@Javawerks: Idunno about mem leaks. IME in such cases it’s always a problem between the screen and the chair. Managed mem languages don’t relieve you from caring about your code’s usage of memory, they just move the problem to a different level.
Hmmm… I don’t think I will be holding my breath for this one. Other than the “import” and maybe the extra string handling capability, there isn’t much here in it for me. What is really needed is high performance implementations to compliment HTML5’s canvas so plugins and flash become a thing of the past.
[...] article is from the ECMAScript 6 looks promising in this article was translated [...]
[...] 本文是从 ECMAScript 6 looks promising 这篇文章翻译而来。 [...]
妈的,好好的JS被改成这个样子,真垃圾!都是些花哨的功能,就不能花点心思在如何提高JS的执行效率上,这样只会把精巧的JS改的越来越庞大和复杂!
I don’t want Javascript to be another Python!!
哈哈
[...] ECMAScript 6 looks promising [...]
[...] 导读:本文是从ECMAScript 6 looks promising 这篇文章翻译而来。译文来自外刊IT评论《JavaScript6看上去很美》。 [...]
usefully,thanks for sharing !!
oh my gosh!
这是要把js改成四不像才罢休吗?
There’s still hope: TypeScript
Regarding let – why use another keyword? Why can var be used for the block scope? Multi-line strings looks ugly to me. Are special chars like \r and \n are so hard to use? And what about class keyword? Will JS became a full-fledged OOL?
[...] think you can try this and other features by first downloading canary chrome and then turning on Enable Experimental [...]
[...] think you can try this and other features by first downloading canary chrome and then turning on Enable Experimental [...]
[...] think you can try this and other features by first downloading canary chrome and then turning on Enable Experimental [...]
[...] ECMAScript 6 Looks Promising [...]