11 ms·
The Future of JavaScript
- ootachi 15y agoFirefox nightlies have all of these features as well. Good to see Chrome following suit.
- karterk 15y agoMinor nitpick - FF has had let scoping for a long time. WeakMap has been around since FF 6[1]. [1]: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/WeakMap https://developer.mozilla.org/en/JavaScript/Reference/Global...
- jashkenas 15y ago> No surprises, no more need to abuse objects as dictionaries. ... pretty much every single file of JS ever written is "guilty" of this.
- Lewisham 15y agoI've only delved into really writing JS for the last couple of weeks, but a number of tutorials basically said that objects are dictionaries and vice versa, you just got some nice syntax and you can plug function in as values. If that was an abuse, what are JavaScript objects supposed to be? What's the difference between them and a dictionary?
- jashkenas 15y agoIt's only an abuse because JavaScript has no distinction between the notion of an object's "meta" properties, and its dictionary-like keys and values. So you'll get along just fine, until someone tries to write this: var dict = {}; dict["hasOwnProperty"] = true; dict["toString"] = "Bob"; dict["valueOf"] = 7; ... then you're in for a rough time.
- mbrubeck 15y agoAnother problem with objects-as-dictionaries is that the keys must be strings. If you use a different type as a key, it is implicitly converted to a string, which can lead to some surprising problems for programmers who don't expect it. Maps/Set/WeakMap properly support arbitrary objects as keys.
- grannyg00se 15y agoAgain, this is something that is simple to learn and understand. And you would learn this in a beginner tutorial. Why go through trouble to change it and complicate the language even further? I undeerstand weakmap may have some real advantage as far as GC, at least.
- batista 15y agoAgain, this is something that is simple to learn and understand. And you would learn this in a beginner tutorial. Why go through trouble to change it and complicate the language even further? For one simple to understand != intuitive or easy to use. Chess rules are easy to understand too, you can learn all of them in an hour, but try to play the game with any quality. So this being simple to understand does not mean its simple to code around and debug. You are also misguided in that this addition "complicates the language". It's not like adding generics or monads or some crazy new feature. It's one of the most fundamental datatypes, implemented properly for the first time. So, instead of saying Object foo; foo.x = "a"; when you need a dict, and having to guard against all consequences, you get to use Map foo; foo.x = "a"; and there is nothing you have to think about anymore.
- ypcx 15y agovar map = Object.create(null); ..will not have any of those properties. Works only in v8 though, AFAIK.
- chc 15y agoI just tested and it looks like it works in SquirrelFish/Nitro and SpiderMonkey as well. Cool trick — I didn't know about that. But I don't think it quite solves the problem jashkenas is talking about, which is that JavaScript automatically calls certain properties of your object behind the scenes in many situations, so you aren't completely free to use any key you want. You might be able to use a no-proto object like this as the internal datastore for a map, but by itself an object with arbitrarily assigned keys would cause a lot of problems unless you were very careful with it.
- ypcx 15y agoAnyone know where can I request a OrderedMap? That committee Wiki seems hardened against intruders. Would be really unfortunate to have to wait for a native OrderedMap/SortedMap/TreeMap until ES7.
- heydenberk 15y agoBring it up on es-discuss https://mail.mozilla.org/listinfo/es-discuss https://mail.mozilla.org/listinfo/es-discuss But please, search the archives first and be aware of what discussions have already taken place on this subject. =]
- heydenberk 15y agoThe post links to an outdated Proxy spec. Here's the right link: http://wiki.ecmascript.org/doku.php?id=harmony:direct_proxies http://wiki.ecmascript.org/doku.php?id=harmony:direct_proxie...
- heydenberk 15y agoAlso, this is a great paper posted on ES-discuss about Proxies: https://mail.mozilla.org/pipermail/es-discuss/attachments/20120208/e3f5075b/attachment-0001.pdf https://mail.mozilla.org/pipermail/es-discuss/attachments/20...
- laktek 15y agoIt seems Chrome actually implements the old proxy API, not the new direct proxy API. It has the old Proxy.create() instead of Proxy.for(), which is the way to define a proxy as per direct proxy API. So if anyone wants to implement proxies in current Chrome, they have to refer to the old proxy API (which is why they have linked to it).
- msie 15y agoIt is due by the end of next year Ouch. I guess the end of 2013 is sooner than never.
- herge 15y ago> (Caveat: Iteration over collections is not yet specified) Is this a joke? Is not iterating over dictionaries and arrays javascript's most glaring weakness? How hard would it be to agree on something like foreach, that, you know, actually works the way you think it should work.
- stevejohnson 15y agoMy guess would be bikeshedding.
- JoshTriplett 15y agoRemember that the spec won't come out for a while, after a pile of additional development. Also remember that we have to live with it for a long time, so taking the time to get it right seems preferable.
- tikhonj 15y agoThere is a foreach that works: .forEach(). And when you're not just iterating over an array but transforming it, you have map(), reduce() and filter().
- herge 15y agoIs it really too much to ask to have some syntactic sugar. Is for i,v in blah , where i is an index of an array, or a key (not a property!) of a dictionary really too much to ask in 2012? No wonder people are using coffeescript.
- jashkenas 15y agoCoffeeScript's got you covered. Here's forEach ... for item, index in list ... here's the same over an object ... for key, value of object ... here's the map() ... mapped = for item in list ... here's the filter() ... filtered = for item in list when item.isActive ... and so on.
- 15y ago
- Siverv 15y agoWould there be any possibility for the addition of native Long numbers to JavaScript, or is this, due to some nature of the language, something that can't, or shouldn't, be included?
- JoshTriplett 15y agoI'd settle for integers. :)
- abecedarius 15y agoYou could add a 64-bit int type, sure, but since implementations use pointer-sized data that has to represent other types as well, making it as efficient as a long long in C isn't as easy. Seems unlikely to muster enough priority. (I haven't been following their work, though.)
- jorangreef 15y ago64-bit bitwise operations would be a big help.
- haberman 15y ago> The ECMA committee is working hard on designing "Committee" and "designing" in the same sentence gives me the shivers. But the features they're describing actually sound pretty reasonable. Actually a lot of them seem to already exist in Lua ("proxies" sound like what are provided by Lua's metatables, and Lua already has weak tables and lexical scoping).
- grannyg00se 15y ago"Now "let" is the new "var" – traditional "var" declarations are complemented with "let" and "const". Both are properly block-scoped bindings, eliminating a common source of errors and weird behaviour." Somehow going from one keyword for variable declaration to three doesn't seem like a good way to reduce errors.
- agscala 15y agoThe extra keywords aren't going to reduce the "errors and weird behavior", the fact that they will be block-scoped and not function-scoped is what will eliminate the confusion.
- ams6110 15y agoI agree with geuis above, JavaScript scope is simple to understand now. There is global scope, and there is function scope. That's it. And there's a certain elegance to that.
- wingo 15y agoThere are more scopes than that: free variables versus locals; eval-introduced bindings; "with"; the exception identifier in "catch"; the function name in named function expressions; argument names; etc. And the scoping rules are quite tricky in practice, to real programmers. To take an example from today: https://bugs.webkit.org/show_bug.cgi?id=27226#c4 https://bugs.webkit.org/show_bug.cgi?id=27226#c4
- grannyg00se 15y agoLooking at that example, I would ask ....where exactly is that function definition getting defined? Why would you put a function definition inside a catch block? Do you think it's getting defined when the catch block is called? Are you programming in Javascript, or are you just writing javascript the way you would write code in some other language?
- aledalgrande 15y agoAs long as this doesn't become another difference between browsers (= headache).
- nas 15y agoEven with optimistic estimates, it's going to be years, likely decades, before you can rely on these features being supported by the majorly of browsers. That's kind of sad but is the hard reality. I hope they come up with a good design. I think they should be very conservative about what they propose. It should be a very obvious improvement to the language or have a lot of design time spent on it. If all major browser developers are not on board (Microsoft?) then the improvement is much less valuable.
- winteriscomming 15y agoIs it really not out for 2 years? Really? That seems like a long time for not that much changes. Any improvement is welcome, but still. I don't know if this will keep pace with Dart and other options. It seems way too slow.
- fourstar 15y agoReally? You seriously think Dart is anywhere close to taking over Javascript?
- winteriscomming 15y agoNo I don't but my point is that 2 years seems a bit slow as far as evolution of JS is concerned.
- tomjen3 15y agoI two years? I hope so (I use Javascript and hate it) but either way 2 years is 17 major releases of Chrome away. Much can happen before then.
- rlf 15y agoFor the love of God, make it more like Ruby. Learning JavaScript after having worked with Ruby is like driving a horse and buggy after you've driven a Ferrari.
- geuis 15y agoI'm probably alone in saying this, but I hope most of this stuff never sees the light of day. What makes JS powerful is its simplicity. "var" or no "var" gets you local or global scope. Done. Want a constant? Make a global variable and don't change it. Want to really make sure its unchangeable? Make an accessor function. Scope in JS is very simple to understand and quite versatile once understood. Adding more layers of complexity does not help. One of the strengths of js is that every thing is an object(almost). So the bit about "no more need to abuse objects as dictionaries" is a bit odd. Since most things are objects, you get the power to treat functions, types, literals, etc in interesting ways. One of the strengths of object literals is that you can use them as dictionaries. obj.foo === obj['foo']. I love getters/setters and some other improvements like strict mode. However I am very much against what seems like an unnecessary cluttering of the language without fixing some of the long standing problems like ==/===. For me, I almost never use == so it should be gotten rid of. Make == === === for example.
- ireadzalot 15y agoIn my experience, the charm of simplicity of JS starts fading away when you start moving away from DOM manipulation and start writing complex objects. Just last week I ran into the lexical scoping issue ( I was not aware of it before) that had me scratching my head for hours. Scoping in JS becomes less fun the deeper you start digging. I am glad that they are aware of it and are actively trying to address it.
- thaumaturgy 15y agoI agree with geuis, and I've written a personal-use JS framework that's in the same ballpark as jquery. I've run into the lexical scoping issue, but not for a long time. Straightforward naming conventions and Firebug can track down scoping problems really quickly. I'm not really opposed to the changes to JS that are mentioned on the Chromium blog, but mostly because I don't have to use them. I don't really see the point of any of them -- and practically speaking, they won't see widespread use anyway until IE 9 dies out -- but maybe I'm just not imaginative enough to understand yet.
- wslh 15y agoFutures like in node-fibers? https://github.com/laverdet/node-fibers https://github.com/laverdet/node-fibers I think it is the proper way to complement generators and give the alternative to wait for a function value.
- zoips 15y agoI don't think there's a reason to bake futures/promises into the language. Once you've got coroutines (generators here), people can build the abstractions around them that they like.
- boyo 15y agoLexical scoping, strict mode, hash tables. Is it just me, or did this post make JS sound a little ... Perlish?
- akavlie 15y agoI had been wondering when Harmony was due. The end of NEXT year? Wow, that's a long wait.
- substack 15y agoI really dislike what they're doing with modules because it's such a step back from the commonjs-inspired require() that node uses. In node, require() just returns a value. That value is usually an object but often it's just a function. Why should a module that encapsulates exactly one function return anything else but that one function? In ES.next, you've got to use pythonesque `import y from Bar` statements which introduce a frustrating correspondence between the lexicals of your own program and the export names of the module you're trying to use which is really terrible if you're using as many modules as I typically do. I presume ES.next will invent even more specific syntax for some sort of `as` like keyword to side-step this but it seems so unnecessary when javascript already has a module system that is so very good as is exploding in popularity and use (>7000 modules now on http://search.npmjs.org/ http://search.npmjs.org/). I understand what they're trying to do with static analysis too but you can already pretty much do that and I've done it. It's not hard at all, just ignore require() statements that don't contain strings when you walk the AST, like this: https://github.com/substack/node-detective https://github.com/substack/node-detective Inventing more syntax instead of just adopting a far better module system that has seen actual use in practical situations is such a shame and ES.next is rife with this kind of prescriptivism. It irritates me to no end and I sometimes want ES.next to die a quiet death in obscurity because of it. http://wiki.ecmascript.org/doku.php?id=harmony:modules http://wiki.ecmascript.org/doku.php?id=harmony:modules
- deleted 15y ago[deleted]
- tjholowaychuk 15y agoI agree. In general unless there is very good reason to do so I think adding to the grammar is the least elegant thing you could possibly do. Like you say it's often very helpful to effectively assign a new name to something you want to require, I do it all the time too. I really prefer the implicit scoping that we have too, not wrapping things in "module Foo { module Bar {} }" blah blah.
- mcantelon 15y agoAttempts to create big iterations on successful languages seem to consistently fail to win adoption.
- firefoxman1 15y agoAfter reading many of really great comments on this post, I think that additions to a language deserve community input. Not just a couple of the "Best" programmers sitting around a table deciding. This is the Web, after all. An interconnected world-wide community. Why isn't there some sort of voting process that anyone can give their input for ECMAScript's drafts? Right now it's an Oligarchy. At the very least, an electoral college-like system would be nice.
- marknadal 15y agoCould somebody clarify, but I feel like I won't ever use this. Older browsers won't understand these new "features", so I still will have to code as I am used to. So I guess in 20 years... I might dabble with them.
- JoshTriplett 15y agoYou can use this any time you know your target environment. For instance, if you write JavaScript for Firefox addons, Greasemonkey scripts, Chrome extensions, or node.js, you know exactly what JavaScript engine you'll run on, and you can take advantage of new JavaScript features that engine implements. You can also use new JavaScript features the same way you use other new browser features like WebSocket or cross-domain xmlHttpRequest: when you've determined that all the modern browsers implement the feature you care about. The browser vendors reach a point where they stop supporting older versions with security updates and similar; I'd consider it reasonable to stop supporting a browser when it stops receiving security updates. (You should also evaluate your audience and the browsers they actually use.) At that point, older browsers get the same experience as links or wget: they get the content, but not the enhancements provided by JavaScript or whatever other features you use.
- batista 15y agoIt would be nice if there was some mechanism --at least for dynamic languages-- where when a feature X (say, the new "let" keyword) is introduced in the compiler, you can add some code that adds support for it at runtime. Maybe with some kind of name-mangling to avoid name clashes with existing variable names, etc. Of course it would incur a performance penalty, and for some stuff it might also be infeasible, but there could be some low level package just for this purpose, that provided options to add new keywords, bytecodes, etc. The end result would be something like (in mock-pyjs glory): if __language__.has_not("let") import __Future__.Let". This would enable faster iterations of the main language, while keeping backwards compatibility. Anyone knows if anything like this exists? (Well, you can do sort of this kind of thing in Lisp. But has it ever been attempted in a C-like syntax?) P.S My understanding is that Python does not do this, it just makes the __future__ imported modules optional in the previous version of the compiler, but they are still implemented in plain C along with the rest of CPython, no?
- JoshTriplett 15y agoPersonally, I'd love to see an implementation of ES.next as a static compiler. Much like CoffeeScript provides a JavaScript-like language that compiles to JavaScript, an ES.next compiler could compile ES.next JavaScript to cross-browser JavaScript.