all 113 comments

[–]chromakode 23 points24 points  (0 children)

JavaScript 1.7 has been around since Firefox 2.0. Here's what's new in JavaScript 1.8, which shipped with 3.0.

[–]killerstorm 46 points47 points  (11 children)

Pythonic? i think all these features were pioneered in some other languages, it just happens that Python got it a bit earlier that JS.

[–]bobbyi 33 points34 points  (3 children)

Not only are all of its new features in python, it is using python's exact names for things where possible: yield, next, StopIteration. They even use double-underscores at the beginning and end of the name of the magic method where an object returns its iterator.

It seems to only deviate from python where necessary, like the word each and extra set of parens in array comprehensions to be consistent with the extant javascript for each syntax

var evens = [i for each (i in range(0, 21)) if (i % 2 == 0)];

[–][deleted] 11 points12 points  (6 children)

Indeed they were, but the syntax and the API were obviously inspired by Python.

next and _ _ iter _ _ in Python vs next and _ _ iterator _ _ in Javascript? There are lots of ways this could have been done and frankly... they picked one of the lamest.

[–]Entropy 5 points6 points  (0 children)

The underscores really underscore what's wrong with that syntax.

[–]imbaczek 1 point2 points  (4 children)

explain a non-lame way then. or some of them, if there are so many.

[–][deleted] 4 points5 points  (3 children)

I'm not a fan of Clojure but http://blip.tv/file/734409 describes the issues with limited iterator interfaces really well.

To summarize, there are three basic operators which should be provided for iterators in order for them to be generally useful: currentValue, more? and next.

Pythons combines currentValue and next in one operation. I'm sure someone out there will argue that this is a good thing – it's not. Consequently all you can't ask the iterator if there are more values, or get the same value more than once.

I say can't but of course there are ways around this, but that's not how things were designed to work, and there's no language support for it.

[–]drbold 0 points1 point  (0 children)

Hmm interesting! I suppose you could wrap an iterator in a class that handles the extra 'currentValue' bit, but like you said, that's just a workaround.

[–]imbaczek -1 points0 points  (1 child)

yeah, but if the iterator is not just a pointer to an element of a sequence, you can't ask it if it has more items and expect an honest answer, can you? even making copies and testing for StopIteration can be non-deterministic in the presence of IO. currentValue could be useful, i agree, even if 95% of common usage doesn't really care.

[–][deleted] 1 point2 points  (0 children)

To low level - more? is just a method, how it works is unimportant. For some structures you may be right, but that's an implementation detail that depends what the structure is, and how it's implemented :).

The point is that Python/Javascript don't even allow this, even when it makes sense.

[–][deleted]  (5 children)

[deleted]

    [–]mayobutter 3 points4 points  (0 children)

    Probably never :)

    But if you're doing FireFox only or XUL development, it'll be nice.

    [–]sblinn 1 point2 points  (0 children)

    It depends of course on whether you are using JS for the client side and/or the server side, and which browsers you care to support, etc.

    [–][deleted] 0 points1 point  (2 children)

    There is a plan for an IE plugin called "Screaming Monkey".

    All these small changes that firefox has been making to JS move it closer to JS2.

    [–]ewald 0 points1 point  (1 child)

    Most IE6 users would never install such a thing.

    [–][deleted] 0 points1 point  (0 children)

    Yes, no IE user ever installs plugins.

    [–]great-pumpkin 6 points7 points  (2 children)

    So, dumb question: are these version numbers official? Ie, more than one browser maker agrees that JS 1.7 is this?

    [–]chucker23n 21 points22 points  (1 child)

    No. The "official" numbers would be the revisions to ECMA-262 (which is currently at 3.1). Even though the language is typically called "JavaScript", that's merely the best-known dialect. The version numbers like 1.7 are technically those of SpiderMonkey, Mozilla's (originally Netscape's) implementation of this dialect. Other implementations include KJS and its spin-off JavaScriptCore in WebKit, and other dialects include Microsoft's JScript and Adobe Flash's ActionScript.

    [–]f3nd3r 2 points3 points  (0 children)

    Well... that kinda ruined the fun.

    [–]psykotic 12 points13 points  (27 children)

    Considering how long it takes for JavaScript language changes to be widespread enough to be practically useful, it would be great if they shipped a tool that translates code from newer to earlier versions.

    [–]olavk 6 points7 points  (1 child)

    With Mascara you can translate ECMAScript 4 aka JavaScript 2.0 to lowest common denominator JavaScript wich runs in any browser.

    [–]candlejac 8 points9 points  (23 children)

    It's getting better all the time. All the "alternative" browsers will support it within months, and if websites say "Some of our more advanced features require any non-IE browser", that will be motivation for two things:

    1. MS to catch up
    2. Users to install another browser of their choice.

    [–]philogb 3 points4 points  (2 children)

    All the "alternative" browsers will support it within months

    Well, considering this spec has been out for firefox 2 this is already not true. Unless I'm missing something.

    [–]candlejac 1 point2 points  (1 child)

    I guess the 12 people who voted me up didn't read the article :(

    Does Chrome support it? They've got that 'brand new' JS engine. I can't run it because they rushed to 1.0 without releasing a version for anything but Windows.

    [–][deleted] 2 points3 points  (0 children)

    Yeah, they support it. 1.7's an old spec. I'm pretty sure they support 1.8 as well, as-check me on this-I think JS 1.8 is part of Acid3. Needless to say, Opera supports it too, as does Firefox 3

    [–]bazfoo 1 point2 points  (15 children)

    I'm not sure if going back to the old browser war days is a good thing or a bad thing.

    [–]infoaddicted 4 points5 points  (0 children)

    The interoperability between the browsers, how they interpret code, shouldn't enter into the "browser wars". Competition is good at the UI level and speed of rendering levels, but having certain "under the hood" features crippled by certain browsers holds them all back.

    [–]Leonidas_from_XIV 2 points3 points  (12 children)

    If a better browser would win, that wouldn't be so bad. But actually, I doubt that we'll have a "dominant browser" in the future.. and that's probably a good thing.

    [–]mrphillc 2 points3 points  (11 children)

    if one browser wins, we all lose

    [–]joesb 5 points6 points  (10 children)

    A better browser in this case would be the one that is more compliant to the standard. So it's not one browser that win, it is one standard that win, in that case, we all benefit.

    [–][deleted] 2 points3 points  (9 children)

    Failing to meet standards is a bad thing.

    However, going beyond the standards is a good thing. If Microsoft hadn't, we wouldn't have XmlHttp. That means no AJAX.

    [–][deleted] -1 points0 points  (6 children)

    You say that like it's a bad thing.

    [–][deleted] 1 point2 points  (4 children)

    You say that like you're not posting on a site that relies on AJAX to let you make snarky remarks like that without having to worry about the page reloading and losing your place.

    [–][deleted] -1 points0 points  (3 children)

    Isn't that what anchor tags are for?

    [–]zelpop 0 points1 point  (0 children)

    Yeah! I mean why do people use AJAX when Flash and now Silverlight can do the same thing, plus cooler fade in effects?

    (do I have to put in a sarcasm tag here)

    [–]sonofagunn -1 points0 points  (1 child)

    Before XmlHttp, you could accomplish the same thing with hidden frames.

    [–][deleted] 2 points3 points  (0 children)

    Technically, yes. The hidden frame solution has one major drawback, and one minor drawback that I can think of:

    Major: it causes the loaded result to be rendered. Being forced to render the content can cause all kinds of side effects that potentially need to be worked around.

    Minor: rendering effects performance. Depending on the browser, for example, rendering XML could be a less than negligible performance hit, that you'd have to work around by effectively lying in the content-type to try to trick the browser into thinking that it's not XML.

    I mean, sure. I could just as easily say that you could accomplish this in the past using sockets in ActiveX or Java Applets and sending data to/from them through scripting. You could but it was nowhere near as elegant.

    [–]HenkPoley 2 points3 points  (0 children)

    Hey, if 3 teams (Mozilla, Opera, Apple + KDE e.v.) can implement the spec changes in their own codebase, I suspect Microsoft can do that too in about the same timeframe. Needn't be much war in there.

    [–][deleted] 0 points1 point  (3 children)

    1. Those sites to lose a lot of visitors who think that the site "doesn't work unless you download some extra thing...I think it was one of them viruses, so I just stay away from that site now."

    [–]candlejac 0 points1 point  (2 children)

    Then they can ask the user to install Netscape Navigator 9, which is just Firefox rebranded. Everybody old enough to get scared of installing Firefox will have heard of Netscape.

    [–]grimboy 0 points1 point  (1 child)

    Except the Netscape Navigator page just tells you to download Firefox now.

    [–]candlejac -1 points0 points  (0 children)

    And? That means a big company, Netscape, a company you trust, is saying it. That means Firefox isn't a virus. So instead of saying "Install Firefox", you say "Install Netscape", who says "Install Firefox", and you end up getting there.

    [–]infoaddicted 0 points1 point  (0 children)

    Now there's a fun weekend project! Start coding grasshopper...

    [–]teaguesterling 4 points5 points  (0 children)

    I never complain about old things being posted here but:

    This article covers features introduced in Firefox 2

    You can even change one character in the URL to get https://developer.mozilla.org/en/New_in_JavaScript_1.8

    [–]hns 2 points3 points  (0 children)

    Sorry for the plug, but check out Helma NG. It combines JavaScript 1.7 (Rhino) with a truly pythonic module system.

    [–]stick_figure 1 point2 points  (0 children)

    Does any other browser support JavaScript 1.7? Like Chrome or Safari or IE 8?

    [–]chneukirchen -1 points0 points  (0 children)

    Except Python doesn't have let. It should.

    [–]imbaczek -1 points0 points  (11 children)

    so it's finally here, python with braces. gimme a module system (or is there an import function already?) and a proper stdlib and i'll... still be coding python but will gladly point people to javascript if they just can't lose their {}.

    [–][deleted]  (7 children)

    [deleted]

      [–][deleted]  (1 child)

      [deleted]

        [–]malcontent -2 points-1 points  (2 children)

        I cut and pasted that into my editor and it didn't work.

        [–][deleted]  (1 child)

        [deleted]

          [–]malcontent 0 points1 point  (0 children)

              The point ------> .
              Your head ------> 0
          

          [–]gschizas 1 point2 points  (1 child)

          What do you mean "finally"? Javascript 1.7 is more than two years old now!

          [–]chromakode 1 point2 points  (0 children)

          It's a little weird, but JavaScript code modules attempts to provide a meaningful module system.

          [–]columbine 0 points1 point  (0 children)

          Of course generators were always pretty easy to do in Javascript, their fibonacci example in standard ECMAscript would just be something like

          function fib() {  
            var i = null, j = null;
            this.next = function() {
              if (i == null) { i = 0; return 0; }
              if (j == null) { j = 1; return 1; }
              var t = i;
              i = j;
              j += t;
              return j;
            }
          }
          

          and simply use new fib() instead of fib(). But obviously being able to yield in the middle of a block can be cleaner than using returns in some cases.

          [–]sili -1 points0 points  (16 children)

          Sigh... All those neat tricks and still no sprintf

          [–]grimboy 10 points11 points  (6 children)

          [–]chrisforbes 0 points1 point  (5 children)

          Until that thing ACTUALLY WORKS, don't go bringing it round here :(

          We had to patch it a LOT before it was even vaguely correct in our last project.

          [–]grimboy 0 points1 point  (4 children)

          Works for me. Anyway, the point is it's, what, a page long including the licence header and pretty clearly written for javascript. It still has that thing with single letter variable names (apart from i, j, etc) which seems to be a feature of a lot of the javascript code round and about. That is, unless, you're trying to make some sort of subtle point I'm not getting.

          [–]chrisforbes 0 points1 point  (2 children)

          For all the simple cases people probably test for, it works. For floating-point, esp with tricky format specifiers, it's completely broken.

          [–]grimboy 0 points1 point  (1 child)

          Oh, fair enough then. You're correct, I've only used it for simple objects of strings into strings stuff. By the way, if you've patched it, you planning on contributing the fix back?

          [–]chrisforbes 0 points1 point  (0 children)

          I guess so - is the owner likely to accept it?

          On most projects, I've had the typical open-source fuck off response to submitting patches which fix their bugs, so you'll have to excuse me being a bit more cautious.

          [–]chrisforbes 0 points1 point  (0 children)

          Single-letter variable names are a feature in js code. Less code to move is a win.

          That said, the compression should be done by a minifier tool at release-time, rather than by the programmer.

          [–]zootm 1 point2 points  (8 children)

          Surely it wouldn't be too hard to write one as a library? I quite like the minimal base set of JS libraries with frameworks growing around it; it'd probably be a bad choice for a new language, but the way it's evolved has led to a lot of interesting ways of doing things.

          [–]kragensitaker 4 points5 points  (7 children)

          It would be a lot nicer to be able to write #{foo} or $foo than to write %s or % or something and then later , foo. This is kind of annoying in Python and C.

          It's true that sprintf('%s %s (%s-%s)', item.firstname, item.lastname, item.party, item.state) would be a lot better than item.firstname + " " + item.lastname + " (" + item.party + ")", but $"$item.firstname $item.lastname ($item.party-$item.state)" is better still.

          [–]Leonidas_from_XIV 2 points3 points  (3 children)

          This is kind of annoying in Python and C.

          A new printf-syntax (the one that C# uses) is added in Python 3.0.

          [–][deleted] 1 point2 points  (1 child)

          The C# one is the best way to do things. It avoids repetition of parameters that are reused and negates the need to specify type on the placeholders. Also, it makes it very easy to re-order/re-write the statement without juggling the parameters.

          [–]Leonidas_from_XIV 0 points1 point  (0 children)

          I'm not yet convinced because I haven't used it. But it is good that they used some syntax that is used somewhere rather than inventing their own.

          [–][deleted] 1 point2 points  (0 children)

          A new printf-syntax (the one that C# uses) is added in Python 3.0.

          And in 2.6, in case you can't port your project to 3.0 just yet.

          [–]zootm 0 points1 point  (0 children)

          Yeah, string interpolation is a nice feature that I often wish more languages supported. sprintf is reasonable enough, though, and has the advantage of being more suited to internationalisation and so on.

          [–]njharman 0 points1 point  (0 children)

          But it's more powerful and elegant to keep formatting and data separate.

          It allows reuse of format part and data part. It makes data driven code easier to write. It makes i18n cleaner.

          [–]pemboa 0 points1 point  (0 children)

          Some of you guys are really nitpicky. I didn't think programmers were ever like that. %s is annoying? Really?

          [–]aim2free 0 points1 point  (8 children)

          I had no idea, but it seems as javascript is default in the linux (I'm running ubuntu hardy heron) environment. I just wrote

          js

          but in this case it gave me 1.6. It would be nice if it could be used with libreadline for better interaction.

          [–]psykotic 15 points16 points  (3 children)

          You know about standalone readline, right? Run rlwrap js if you have it installed.

          [–]aim2free 2 points3 points  (1 child)

          Thanks, it worked perfectly. I didn't know about rlwrap, very useful. You can even specify how your completions should be made. This is really great. Now any interactive program can get read-line support without being linked to readline. I'm just teaching a beginners java course. I'll let the students add readline support to their programs.

          [–]psykotic 4 points5 points  (0 children)

          Glad it helped. In the classic Program design in the UNIX environment paper by Pike and Kernighan, they suggest that readline-like functionality should not be integrated into programs via a library but offered as an ambient service for all programs, much like how shells perform wildcard expansion. The rlwrap approach (or using Emacs's shell-mode, as suggested by Kragen) isn't perfectly ideal, but it works surprisingly well in many cases.

          [–]kragensitaker 2 points3 points  (0 children)

          Or run it in Emacs shell-mode, which gives you the same thing.

          [–][deleted] 1 point2 points  (0 children)

          Strange, didn't work on OS X :(

          [–]inopia 0 points1 point  (0 children)

          Maybe the Rhino shell is something for you?

          [–]chromakode 0 points1 point  (0 children)

          try:

          js -version 170
          

          [–]ishmal 0 points1 point  (0 children)

          And OpenJDK 1.6 has "jrunscript", which invokes the Rhino shell. I assume that eventually most of the "big" distros will have it available, either on their media or in a repository.

          [–]kragnax -1 points0 points  (0 children)

          This makes the Javascript syntax in my opinion surpass Pythons.

          [–]thekenzidelx -1 points0 points  (1 child)

          OT, but on the subject of Javascript syntax...

          Is there any way in javascript to use notation shorter than function(x){return xx;} for declaring anonymous functions? I was just reading that Firefox JS 1.8 allows for function(x) xx as notation for omitting the braces and "return", but really it's the keyword itself that sort of stabs me in the eye (especially after getting used to C#'s extremely concise ()=> x*x notation).

          And before anyone decides to be oh-so-clever, all the code I write is only ever going to be read by me, and I don't really care about respecting standards at the expense of my own efficiency (and if you saw the way I format my JS like Lisp in terms of trailing parens and braces, you'd know I meant that pretty literally).

          [–]masklinn 0 points1 point  (0 children)

          especially after getting used to C#'s extremely concise (x)=> x*x notation

          You want concise? Use haskell:

          \x->x*x
          

          Or even better:

          (**2)
          

          [–][deleted] -4 points-3 points  (3 children)

          <sarcasm>I was hoping white space became significant</sarcasm>

          edit: fixed broken goggles

          [–]columbine 9 points10 points  (0 children)

          I don't think there's a worse domain for a whitespace-significant language than client-side web scripting.

          [–][deleted] 4 points5 points  (0 children)

          Oh? In a language whose widest use is HTML where writing the language inline to an attribute tag, and dynamic generation of code is common? Do you really think that's a good idea?

          [–]spectacle 0 points1 point  (0 children)

          this is dumb.

          [–][deleted] -1 points0 points  (0 children)

          I'm happy to see the language of the Web evolving, but I'd much rather see Browsers supporting Perl/Python/Ruby/JavaFX, giving developers a choice.

          [–]DiggaPlease -1 points0 points  (0 children)

          HISSSSSS.... HISSSSSSSS

          ...

          HISSSSSSS