My ETech JavaScript tutorial
I gave a three hour JavaScript tutorial at ETech this morning, aimed at people with previous programming experience who hadn’t yet dived deep in to JavaScript as a programming language. It seemed to go pretty well—some good questions were asked at various points and a few people told me afterwards that they had found it interesting.
I didn’t finish the presentation in time to get handouts made up—in fact I was adding the finishing touches 15 minutes before the session began—so I’m posting the slides here. The 111 slides (reduced to low quality JPEGs with Keynote to save on bandwidth) start here: A (Re)-Introduction to JavaScript.
For the sake of completeness, I’m also releasing the notes I made in preparation for the talk (PDF). These are pretty rough and don’t match what I actually said very closely but they might be of interest in any case.
The photos in the presentation were all found using Flickr’s awesome Creative Commons search feature.
Update: I’ve converted the notes to a single HTML file with permalinks for each of the sections: A (Re)-Introduction to JavaScript (HTML).
There’s a lot of interest in the slides as a single file. The PDF is 50MB thanks to all of the images, but I’ll see about getting it hosted on the ETech site. In the mean-time, I’ve posted higher quality copies of the slides to Flickr.
Joe Healy - 7th March 2006 09:22 - #
It seemed to cover quite a lot of useful pointers regarding JavaScript in an easily digestible manner - good stuff.
Robert Wellock - 7th March 2006 11:40 - #
Malcolm Forbes - 7th March 2006 12:42 - #
Martin Albisetti - 7th March 2006 12:43 - #
Anders Rasmussen - 7th March 2006 14:39 - #
GaZ - 7th March 2006 15:07 - #
Andrew Dupont - 7th March 2006 15:44 - #
Very nice presentation. It must have been because I read the whole thing when I knew most of it already.
I like the way you aren't afraid to say "JavaScript isn't perfect; this bit sucks" mainly because this is my personal opinion too. I use it loads but it does have some serious flaws.
One critism is that you didn't emphasise the difference between reference and primitive types. When I was learning JS is took me ages to get used to objects and arrays always being passed by reference. For those of you who don't know:
var foo = {a:1,b:2};var bar = foo;
bar.a = 3;
alert(foo.a == 3); // outputs true
Oliver Saunders - 7th March 2006 18:13 - #
Clint - 7th March 2006 18:49 - #
Royce - 7th March 2006 20:07 - #
Simon Willison - 7th March 2006 22:35 - #
Andrew Phillipo - 7th March 2006 23:59 - #
Steve Iverson - 8th March 2006 02:21 - #
Gary Capell - 8th March 2006 13:15 - #
Troels - 8th March 2006 23:13 - #
function avgArray(arr) { var sum = 0; for (var i = 0, j = arr.length; i < j; i++) { sum += arguments[i]; } return sum / arr.length; }I think the use ofarguments[i]is a mistake; it should bearr[i]Neil Katin - 9th March 2006 17:51 - #
Jon - 9th March 2006 19:44 - #
Avi Flax - 9th March 2006 21:23 - #
gkumar - 10th March 2006 05:24 - #
enej - 11th March 2006 01:46 - #
My programming experience is mainly PHP4 and C++ and they both pass by value unless told otherwise. I would be using PHP5 were it not for my backwards hosting company.
Oh Absolutely I don't depute that. I just found the change very unnatural.
Oliver Saunders - 12th March 2006 00:44 - #
Many thanks for putting this excellent (re)introduction to Javascript tutorial.
There are couple of spelling mistakes in the notes, and another thing worth mentioning about the access to an object's property.
The obj[name] method of accessing an object's property is very useful since even expressions are allowed there. I've seen people using eval() for accessing an object's property since not many know that properties can be accessed using the square bracket ([]) notation.
Gopalarathnam Venkatesan - 12th March 2006 05:02 - #
cherouvim - 16th March 2006 14:25 - #
Srikanth - 20th March 2006 03:47 - #
Michael - 21st March 2006 22:24 - #
Cletus - 29th March 2006 13:16 - #
Hi Simon
Thanks for posting the slides, sorry to have missed your session. I've noticed that there seem to be two different idioms for the Singleton pattern (see below). Am I right in thinking they are equivalent and do you have any particular reason for preferring one over the other?
All the best
--
petef
//Javascript singleton pattern // DHTML Kitchen style // http://dhtmlkitchen.com/learn/js/singleton/ var dk = new function(){ // public method this.foo = function(){ return "Foo!" + bar(); }; // private method function bar(){ return "!Bar" } } // Simon Willison style // http://flickr.com/photos/simon/109341462/in/set-72 057594077197868/ var sw = (function() { function foo(){ return "Foo!" + bar(); } function bar(){ return "!Bar"; } // public methods return { 'foo': foo } })()Peter Ferne - 31st March 2006 11:40 - #
I also quite like...
// Hybrid style var pf = new function(){ foo = function(){ return "Foo!" + bar(); }; bar = function(){ return "!Bar" } // public methods this.foo = foo; }Peter Ferne - 31st March 2006 11:45 - #
Anatol Ulrich - 9th April 2006 11:08 - #
Simon, thanks for the singleton pattern. I spent days searching for that after reading it in your slides. I don't believe it made it into your pdf notes. ;)
Peter, not positive on this, but Simon's pattern may be more efficient due to how function objects are evaluated at runtime.
Logic - 20th April 2006 17:03 - #
tom - 29th April 2006 21:20 - #
Hello Simon. Thanks for the singleton pattern.
I think we can combine the Simon Willison style with the DHTML Kitchen style. In my opinion the combined styles produces the most maintainable code due to the fact that we do not have to return every method that we want to expose. As a bonus we also get the most efficient function object:
var sw_dk = ( function() { // Private variables var a = 'Hello '; // Public variables. this.b = "I'm public"; // Private methods bar = function(){ return 'Bar!'; }; // Public methods this.foo = function() { return(a + bar() ); }; // ------------------------------------ // Expose public attributes and methods // ------------------------------------ return ( this ); } ) (); //~anonymous function immediately invoked with () alert(sw_dk.foo()); // --> "Hello Bar!"Regards
Leif Olsen
Leif Olsen - 3rd May 2006 16:24 - #
BEWARE! Not everything you read in blog comments is working as expected!
This is true regarding the suggestion of Leif Olsen:
Using
thisinside the function created without new defeats the purpose asthispoints to thewindow object!This is why in Leif's example
sw_dk == windowyields true! All the public functions end up in the globalwindowobject. This is why the suggestion of Leif Olsen does not work.Daniel Kabs - 4th October 2006 09:53 - #