Monday, February 20th, 2006
The Future of JavaScript: an Update from Brendan Eich
>Brendan Eich has posted a status update on some of the work going into the upcoming JavaScript 2, aka ECMAScript Edition 4 (ES4). One feature that should look familiar to Python hackers are generators and iterators, as seen in the following example taken from a console session:
-
js> function count(n) {
-
for (var i = 0; i <n; i++)
-
yield i;
-
}
-
js> g = count(10)
-
[object Generator]
-
js> g.next()
-
0
-
js> g.next()
-
1
-
js> two_to_nine = [i for i in g]
-
2,3,4,5,6,7,8,9
-
js> squares_to_20 = [i * i for i in count(20)]
-
0,1,4,9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361
Why go with the Python style and syntax?
Given the years of development in Python and similarities to ECMAScript in application domains and programmer communities, we would rather follow than lead. By standing on Python’s shoulders we reuse developer knowledge as well as design and implementation experience. The trick then becomes not borrowing too much from Python, just enough to gain the essential benefits: structured value-generating continuations and a general iteration protocol.
Brendan also mentioned that support for XUL in Python is almost done, and should land back into the main Firefox codebase from the DOM_AGNOSTIC2_BRANCH soon.
The official spec that will lead the way is ECMA TG1, which is being worked on by Brendan along with colleagues from Mozilla, Adobe, and Microsoft. Lets hope Brendan's vision for JavaScript's future will become reality:
My gut says that we will successfully evolve JS into a much stronger language, with a better and more standard library ecosystem, for the next ten years of its life. I will wager that other browsers will follow the ES4 standard, possibly even in 2007.
Related Content:











If you want JavaScript iterator functionality today, take a look at the Mochikit Javascript library. It’s also based on Python, so the ideas should be very similar.
I was kind of hoping for Ruby-like blocks and iterators.
[1, 2, 3].each(function(item) { /* mooo */ });
[JavaScript / Python] JavaScript Gets More Pythonic
[via Ajaxian] Brendan Eich has blogged a status report on the progress of work on the upcoming JavaScript 2 (a.k.a. ECMAScript 4).He talks about an upcoming feature: Python-style generators, iterators and list comprehensions in JavaScript. His examples…
Microsoft was on the spec for E4X as well and even wrote an E4X engine for IE but it’s still sitting on the shelf…
I believe Mozilla has already added some Lisp-like blocks and iterators, like Array.map. These were only added recently, though.
Why don’t we solve the problem and make JavaScript 2.0 = Python ?
Seems to solve a lot of issues.
=?utf-8?B?ZGVsLmljaW8udXMvcG9wdWxhcg==?=
Prototype Dissected
http://www.snook.ca/archives/000531.php
73 Resources on Windows opensource and/or freeware – Listible!
http://www.listible.com/list/w…ws-opensource-and2For-freeware &nbs…
that js shell looks cool just like the python one. how do i get it?
Mike – there’s just that minor piffling detail with backward compatibility :). Besides, current Javascript actually has working closures and true anonymous functions; Python doesn’t.
Daniel Schierbeck:
I also wish they’d include Ruby-like blocks and iterators… In the meantime, I like to use this one-line each for my JS code:
function each(a, f) {for(var i=0; i<a.length; i++) f(a[i])};which allows one to write something similar to what you wanted:
each([1, 2, 3], function(e) { alert(e) })Pathetic.
Here’s another vote for Ruby-esque blocks and iterators.
1 vote against ruby iterators.
Ruby-like Blocks and iterators are possible today:
Array.prototype.each = function(f) {
for (var i = 0; i
Sorry, once again:
Array.prototype.each = function(f) {
for (var i = 0; i < this.length; i++) {
f(this[i]);
}
};
[1,2,3,4].each(function(item) { alert(item)})
“that js shell looks cool just like the python one. how do i get it?”
I think it’s from the Rhino project (requires Java to run and Ant to build).
http://www.mozilla.org/rhino/download.html
Ignore that build thing. You can run it with already built js.jar via “java -jar js.jar”.
Mathias Karstädt:
Yes, you could have a ruby-esque each in JS by making each a method of class
Array. The problem with that approach is that there are other important collection of objects one would also like to traverse with each. For instance, achildNodesreturns aNodeListobject in Firefox (and who knows what in IE), anddocument.getElementsByTagNamereturns objects of a class I haven’t been able to ascertain (theoretically it should be aNodeListobject too but that’s false).You could try to solve this, somewhat rough-handedly, by making each a method of class
Object, but while that will work in Firefox it won’t in IE.Ruby solves this problem by having an Enumerable class of which every collection class is a children, but to my knowledge there’s nothing like that in JS right now.
And that’s why I prefer to have each as a top-level method, as I described in a previous comment.
Eliazar: You might be interested in Prototype’s enumerable: Eliazar, it adds an enumerable support to javascript. I don’t think that collections like nodeList would have them builtin, though its possible with some of the core object extensions that prototype does.
Enumerable doc here: http://www.sergiopereira.com/articles/prototype.js.html#Reference.Enumerable
Rob,
Thanks for the pointer! I was interested, of course, and I’m quite impressed with Sam’s implementation of Enumerable, it’s elegant and complete. I guess this is the passage that explains the Prototype-way to handle the collections I was mentioning:
This is all great, and I’ll be using $A() a lot, but I wonder why Prototype doesn’t include the Enumerable methods inside such collections by default (as it does for Arrays)…
(Oh, and a little bug report: you can’t see a blockquote’s text in the Live Preview, it’s the same color as the background.)
regarding, Ruby-like blocks and iterators.
Javascript has prototype based class, which make it possible for javascript behave like Ruby, Python, Scheme..etc….
Array.prototype.reduce=function(func) {
var l=this.length;
var s=”;
for (var i=0;i
[...] Ajaxian » The Future of JavaScript: an Update from Brendan Eich (tags: ajax Programming javascript) [...]
[...] “Given the years of development in Python and similarities to ECMAScript in application domains and programmer communities, we would rather follow than lead.” (via:reddit) [...]
“Ruby solves this problem by having an Enumerable class of which every collection class is a children, but to my knowledge there’s nothing like that in JS right now.”
Actually thats not quite right. Enumerable is a module/mixin that is imported into certain classes. Theres a difference.
[...] In a recent post on by Brendan Eich on his Brendan’s Roadmap Updates Mozilla blog and echoed on Ajaxian it would appear that our favourite programming languge is about to land on the desktop in the Firefox browser in more ways than one. [...]
Luke,
Heh, I knew someone would point that out… You’re correct, of course. It’s just that sometimes I prefer to be approximately right, rather than exactly wrong.
[...] A summary of the future holds for the javascript language from Brendan Eich. Thanks Steve! [...]
Cool, Python with braces. Now that wasn’t so painful, was it?
Note that the Flash 8.5 player out in beta (with Eclipse-based IDE for Flex) has a sophisticated JIT VM based on ECMAscript 4. (Macromedia helped drive the spec, in fact.)
/not into platform religious wars, use whatever makes you productive.
Man, I haven’t used python in a while. I guess i better brush up on it :)
[...] Ajaxian FirefoxAs soon as Firefox 1.5 comes out, people want to play with some of the shiny nobs. SVG: How Did The Moon Get Into Orbit? One of the early post-release examples of SVG support on Firefox 1.5 was this [...]
The Future of JavaScript.
http://ajaxian.com/archives/the-future-of-javascript-an-update-from-brendan-eich
I’m a big fan of JavaScript….
I thought they said ECMA4 is going to look like Actionscript 3???