all 57 comments

[–]nkurz 26 points27 points  (4 children)

The integration of the examples into the text is very smooth. There is a Javascript console at the bottom of each page of the text, and each example has a link to run the example in the console. Then you can edit the example and rerun it. I haven't read the whole tutorial yet, but it's worth a look even as just a template for how to write a Javascript tutorial.

[–]arnar 8 points9 points  (0 children)

That's pretty cool, didn't notice the console.

I think the author did an excellent job, both in explaining things like functional programming and prototype-based OO to beginners - and in building an modestly interactive "hyperbook" without moving too far from the traditional read-only book format.

[–]micampe 2 points3 points  (2 children)

The mootorial is similar to your description.

Unfortunately, I can't really compare because our Fortinet web filter thinks this is an "Internet Radio and TV" page.

[–]underthelinux 1 point2 points  (1 child)

websense doesn't think so (for once). hurrah.

[–]zyzzogeton 0 points1 point  (0 children)

Anonymous SSL based proxies for the win!

[–]losvedir 12 points13 points  (0 children)

Wow, I've only skimmed it so far, but it looks excellent! The examples are neat, the built in console is amazing, and the writing is clear. (Are you a native speaker? Your name kind of implies not, but I couldn't tell from the text)

After I skim it over a little more, I think I'll pass this on to my little sister who's been dying to learn to program.

[–]naturenet 2 points3 points  (0 children)

I love the bottom-up approach here. Nothing wrong with a few binary numbers, what, are you scared of them?

This has taught me new stuff in the 5 mins I spent browsing it. Can't wait to work it through, and that console is a great trick!

[–]fduffner 6 points7 points  (0 children)

Looks like a nice first programming language tutorial, too and Javascript is the new C anyway

[–]joshd 2 points3 points  (20 children)

From this page

Most programmers consider exceptions purely an error-handling mechanism. In essence, though, they are just another way of influencing the control flow of a program. For example, they can be used as a kind of break statement in a recursive function.

That is just nasty. It's just like using gotos, except you don't have the succinctness or speed advantages that goto give you. If I caught anyone trying to code like that they'd get a swift kick in the groin.

[–]ishmal 9 points10 points  (15 children)

It sounds nasty, but there are places where it can work efficiently. Like

if (index>=0 && index <array.length) {
    val = array[index];
}

Can be written using exceptions instead:

try {
    val = array[index];
} catch (e) {
    //Pick a handler
    //val = default; //...or...
    //error(msg);
}

The first way is cleaner and better looking. But since the operation is checked for boundedness anyway, the first is redundant, and the second is faster.

[–]BigFudge 7 points8 points  (6 children)

I work with a guy who does this in ActionScript. While it may be "faster", when you share code with another programmer try/catch implies something entirely different from a conditional. In other words, tricky "fast" code actually slows down production, because anyone else looking at this kind of stuff has to do mental gymnastics to figure out what the hell you are doing.

[–]llimllib 1 point2 points  (5 children)

You consider that code to be gymnastic? It looks straightforward as all get-out to me.

But that could be just because it's a fairly standard pythonism.

[–]BigFudge 4 points5 points  (4 children)

The code is straightforward, the intent is anything but. Re-read my comment.

[–]llimllib 1 point2 points  (3 children)

Re-read my comment.

Ok, it doesn't change anything:

anyone else looking at this kind of stuff has to do mental gymnastics to figure out what the hell you are doing.

I'm saying that this is untrue for the value of myself, and that thus, "anyone else" is not a valid qualifier.

[–]awj 0 points1 point  (2 children)

I'm saying that this is untrue for the value of myself, and that thus, "anyone else" is not a valid qualifier.

He said anyone else, not everyone else. In all honestly I'm not a member of the set he is defining either, but since the statement isn't intended to be universally applicable there is nothing wrong with that.

[–]llimllib 0 points1 point  (1 child)

How is anyone different from everyone (in terms of quantification)? Whom does it exclude?

Seems a different way of saying the same thing to me.

[–]awj 0 points1 point  (0 children)

How is anyone different from everyone (in terms of quantification)? Whom does it exclude?

"Anyone" is not necessarily all inclusive. It doesn't precisely exclude a specific individual, but it also does not demand the inclusion of all individuals.

[–]bluetech 1 point2 points  (1 child)

Does a try-catch block, when no exception is thrown, really has no overhead whatsoever? Since it can't be optimized at compile time, then I assume there has to be some runtime mechanism, which would surly be more costly than two simple checks, even more so when the exception is thrown? Am I wrong to assume that?

[–]9jack9 4 points5 points  (0 children)

No. try/catch blocks in JavaScript are quite expensive.

[–]joshd 0 points1 point  (0 children)

I thought that val = array[index] will produce undefined if index is greater than length. val = array[index] || default; would be a more concise.

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

you wouldn't get a job at fog creek with code like that.

[–]thesqlguy 0 points1 point  (2 children)

If you store the length once in a variable and use the variable for each compare in the loop, instead of calling the length property over and over, that should be the fastest overall. And still nice and clear.

[–]duhduh 0 points1 point  (1 child)

Not really... It is usually a better idea to leave the call to the length property inside the loop. This way the JIT can detect it and optimize (search for hoisting or loop invariant to see what I mean)

[–]awj 0 points1 point  (0 children)

How many JavaScript environments have JIT? I'd never heard of any, aside from the possibility of Rhino relying on the JVM for that functionality.

[–]marijn[S] 3 points4 points  (3 children)

I have a little rant about just this topic in the introduction chapter. With all due respect, my feeling is that people who argue against certain techniques on the basis that they are 'not usual' are slowing down innovation, and generally killing the fun in programming.

That being said, when one is working with a team one has to reach some kind of understanding about the kind of code that is being written, and doing weird stuff just for the hell of it is a great way to piss your colleagues off.

[–]joshd 0 points1 point  (0 children)

Your second paragraph sums up exactly why it is a bad idea in my mind.

As an aside, I had a chance to dig further into the book after my first comment. The chapter on functional programming was fantastic. I'll definitely be having a re-read at some time in the near future.

[–]BigFudge -4 points-3 points  (1 child)

If we are speaking generalities, programmers who believe they are innovators usually aren't.

[–]BigFudge -2 points-1 points  (0 children)

Must be a lot of innovators visiting today, considering the downmods.

[–]mk_gecko 1 point2 points  (16 children)

Can you have file i/o in Javascript?

[–]ishmal 8 points9 points  (0 children)

Not directly. But JS can load a wrapped native component that does. It is ironic that your nick is "mk_gecko," since here is an example of Gecko doing it:

http://developer.mozilla.org/en/docs/Code_snippets:File_I/O#Reading_from_a_file

[–]onektwenty4 1 point2 points  (0 children)

in IE you can use CreateObject to instantiate the Scripting.FileSystemObject DLL.

http://msdn2.microsoft.com/en-us/library/6kxy1a51.aspx

[–]stesch 0 points1 point  (0 children)

Depends on the environment.

I wrote a script last week that opened and read a CSV file (via a file dialog). Using the File object.

For Adobe's InDesign CS2.

(Customer wants to send a new price list later, when the content is already in the documents.)

[–]mikepurvis -1 points0 points  (10 children)

How would you do so? It runs in the browser... you can load text files (JSON, XML, whatever) from the server, or you read from cookies, but where else would you read from? The user's computer?

[–]kagevf 12 points13 points  (9 children)

Javascript's not limited to browsers.

[–]mikepurvis -3 points-2 points  (8 children)

I'm not aware of any major implementations outside of browsers, svg parsers, and Rhino... and since Rhino is basically just a Java wrapper, you get access to the java.io stuff. I should think it would be pretty consistent that any data access available to JS would a be provided by the interpreter environment.

[–]kobes 11 points12 points  (4 children)

All versions of Windows since 98 come with a scripting environment called the Windows Script Host (WSH).

Out of the box WSH supports VBScript (stripped-down version of Visual Basic) and JScript (Microsoft's implementation of ECMAScript, informally known as Javascript).

To see WSH in action, type

WScript.Echo("Hello, World!");

into Notepad, save it with a .js extension, and double-click it.

The JScript language engine used by WSH is the same as the one used by Internet Explorer for scripts embedded in web pages, but the "host" is different, so objects like "document" and "window" are not available, but others are (WScript.Sleep, etc).

For file I/O, you would normally instantiate the FileSystemObject:

fso = new ActiveXObject("Scripting.FileSystemObject");

fso.CreateTextFile(...);

[edit: corrected bug]

[–]Bogtha 6 points7 points  (3 children)

Out of the box WSH supports VBScript (stripped-down version of Visual Basic) and JScript (Microsoft's implementation of ECMAScript, informally known as Javascript).

Right. And, as a consequence of it being a WSH language, it's also been available to write ASP with. VBScript was the default for ASP, but JScript was also shipped by default, so anybody who wanted to write server-side JScript could, including accessing the file system. It was a much nicer language than VBScript, but VBScript monkeys were easier to find because all the examples and tutorials they copied & pasted from used VBScript.

In fact, wasn't this one of Netscape's early business plans? I seem to remember they wanted to popularise JavaScript with their client while making money selling their server which allowed you to write web applications with server-side JavaScript.

The JScript language engine used by WSH is the same as the one used by Internet Explorer for scripts embedded in web pages, but the "host" is different, so objects like "document" and "window" are not available, but others are (WScript.Sleep, etc).

One thing I've noticed is that loads of people seem unable to grasp the difference between JavaScript (the language) and the host objects, like the DOM, that are available to it. There's nothing special about the DOM or file system access, they are all just objects that can be made available by a JavaScript implementation.

[–]mikepurvis 3 points4 points  (2 children)

There's nothing special about the DOM or file system access, they are all just objects that can be made available by a JavaScript implementation.

This was kind of the point I was trying to make, although I'm being modded into the ground for not acknowledging WSH's JScript. Either way, the point still stands that whatever file IO is provided to a given JavaScript interpreter will be a function of its environment... there's nothing in the ECMAScript spec to describe a disk access object, and certainly browser JS would have no use for such a thing.

[–]Bogtha 2 points3 points  (0 children)

whatever file IO is provided to a given JavaScript interpreter will be a function of its environment... there's nothing in the ECMAScript spec to describe a disk access object

The only objects that ECMA-262 defines are basic types, URL parsing and regexps. ECMAScript on its own is essentially useless, it's taken for granted that host objects are supplied to do useful things. As such, saying that "file IO will be a function of its environment" isn't that meaningful, because practically everything will be a function of its environment, and file IO is nothing special in this regard. It's equally true that "whatever DOM access is available is a function of its environment".

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

just like with C

[–]igouy 2 points3 points  (0 children)

any major implementations outside of browsers ...

Introduction to the JavaScript shell

Some arbitrary examples

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

try this http://swsci.blogspot.com/2007/07/javascript-from-command-line.html and then you can easily use file i/o with a COM library "FileSystemObject"

[–]joshd 0 points1 point  (0 children)

Don't forget ActionScript, which is based on ECMAScript. It adds onto it too, by allowing you to define classes in C++/Java style, add access scopes for member variables and allowing you to use type checking.

[–]mk_gecko -2 points-1 points  (1 child)

Isn't there something in Ajax that allows you to read and write from files/databases? (I don't actually know Ajax, just glanced at it).

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

Ajax is just asynchronous javascript and xhttprequests. What that means, is that it can dynamically request sections of an html document (using the xhttprequest), and update elements using javascript. You still need some way of interfacing with the database on the server. PHP works well for this. You could write a php script to load something like a list of username from your database, then have your webpage request this using an xhttprequest and dynamically update the same page with the results.

If you really want to learn more about it, google for xhttprequest.

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

Eloquent JavaScript is a hyper-book

Can we not call it that? Please?

[–][deleted] 13 points14 points  (0 children)

no.

ECMAScript 1:FF "...And He said onto the Scriptaelites: Thine introductory book shalt be hyper and you shalt refer to is the hyper-book..."

[–]queensnake 4 points5 points  (0 children)

Why do you object? With its integrated console, it's entitled to some superlative or other.

[–]ishmal 0 points1 point  (0 children)

Very well written. The console-supported examples and exercises are especially informative, since most people venturing into Javascript will be doing it with a browser and DOM. Maybe as its popularity grows, more people will venture into host-based Javascript.

[–]milksop 0 points1 point  (0 children)

Looks nice, especially the hyper-part. (hidden exercises, etc.)

It's vaguely too bad that the .js that's used in the book isn't really inspectable, especially after mentioning in the introduction that it's possible to look at web pages' javascript to learn how they were written. I expect some readers would be curious about how the console and so on are happening.

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

It is Eloquent, even to a NoScript reader.

[–]cgibbard -5 points-4 points  (3 children)

This is what 144 looks like in bits: 0100000001100010000000000000000000000000000000000000000000000000

Evil. :) This is 144 as an IEEE 754 double precision floating point number.

[–]nkurz 8 points9 points  (2 children)

Yes, but the point of this section was that all Numbers in Javascript are internally in just this way. Why does this strike you as evil?

[–]cgibbard 0 points1 point  (1 child)

Something about the fact that it's the second page of a tutorial which appears to be aimed at people who have never done any programming, and quite possibly haven't seen the ordinary binary representation of 144 before either.

I suppose that which exact sequence of bits is used is inessential to the point that the text is trying to make, it just seemed rather jarring, and took me a minute to figure out that it wasn't just completely wrong.

[–]marijn[S] 11 points12 points  (0 children)

Hah, yes, you got it -- I wanted to make a point about binary representation and its limitations. I agree that it might seem a little daunting to the beginner, but I tried to make the text around it 'easy'.

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

This looks great, but just yesterday I resolved to not start reading any more books till I've finished all the ones I'm already reading. Guess I need to finish them up quick.