I might as well come right out and say it: I’m starting work on my second JavaScript book. My first one was something of a success; it has gotten great reviews and is in its 3rd or 4th printing (I lose count). I’m in the process of pulling together materials, building outlines, and talking with publishers to make this next one happen.
I have another question, related to my previous one, that’ll help me to sculpt the direction of the book.
What are the greatest untold secrets of JavaScript programming that you wish were thoroughly debunked and explained?
I’d like to keep things rather specific and precise, rather than generic. For example, I’m looking for secrets like “How do I get my animations to be so smooth?” rather than “How do I make an awesome accordion menu?”. Questions about how specific libraries do things is perfectly fine, too (doesn’t have to be just jQuery – I’m well versed in most of the major libraries).
Here’s some that I’ve noticed:
- What is
(function(){ })()
and why is it so fundamentally important to modern JavaScript development? - What does
with(){...}
do and why is it so useful? - How can
arguments.callee
change how I work with JavaScript code? - How exactly do timers work and how can I best use them?
- How do I identify and tackle memory leaks in web applications?
- How do I write a cross browser way of…
- Getting/setting attributes.
- Injecting HTML strings.
- Getting/setting computed css values.
- Managing DOM events.
- Writing a CSS selector engine.
- Doing smooth animations.
- How can I use verification tools (like JSLint) to my advantage – and write my own?
- What’s the best way to transmit JavaScript files?
- How do I write my own JavaScript compressor (like Packer)?
All of these are giant gaping holes in the world of JavaScript literature and it needs to be fixed. Are there any that I missed?
Aside: I’m phenomenally bored with traditional Ajax/DHTML books, large parts of mine included. There’s no great mystery concerning the DOM API or how to do a simple XMLHttpRequest – and I hardly think there needs to be considerable literature dedicated to it. However, no one talks about how incredibly hard it is to get those things working in a bullet-proof cross browser manner. Probably the best resource we’ve had was Quirksmode – but that only told you what was broken – but not even necessarily how it was broken and definitely not how to fix it. There needs to be volumes written on how to fix real-world JavaScript bugs, constantly being updated to handle the changing browser landscape. Depending on how progress goes with the planning of my book, I’d like to tackle a large number of these issues directly and get them out there so that everyone can learn from all the work that’s being done.
Gopal Venkatesan (December 31, 2007 at 1:05 am)
I would consider the “with” statement in JavaScript to be very harmful rather than being useful.
It has a side effect of creating global variables if you (mistakenly) set a property that does not exist.
Considering the following piece of code:
var foo = { bar: "baz" };
with (foo) {
bar = "quux"; // ok
baz = "quux2"; // typo, accident!
}
Just my two cents.
Stefan Hayden (December 31, 2007 at 1:09 am)
I don’t know if this is too broad but I think some talk about building classes in javascript would be good. For some reason they still confuse me in JS. though perhaps I could just put more effort in to learning about them.
John Resig (December 31, 2007 at 1:09 am)
@Gopal: There’s a ton of things in JavaScript that can be “considered harmful”. Since JavaScript is such an, extremely, flexible language you can make mistakes all over the place and not catch it. I just think that we need to have a better understanding of how the existing features work to give us a clearer way to move forward and to work with what we have.
(Also, in your example, it doesn’t matter that you’re using a with statement there – having a typo in your assignment will assign the variable to the global scope (if it wasn’t defined elsewhere) in all cases. This is a case where static analysis pays off. It’s too bad that JSLint sticks it’s head in the sand and hates on with.)
@Stefan: That’s a great example, as well. There’s a huge number of ways to do class-like implementations in JavaScript. I covered some of them in my first book, but from a very high level (just looking at how to use the existing APIs, not how they were built). Great idea!
Nicholas C. Zakas (December 31, 2007 at 1:54 am)
Boy, with this announcement, perhaps I should think twice about sharing preview chapters of my new book with you. ;-)
Gopal Venkatesan (December 31, 2007 at 2:10 am)
@John: your point is taken.
I’m just trying to say that people from VB/VBScript land might think that the typo (or rather being aware) might create/add a new property to the object.
I’m particularly highlighting the “with” statement since the concept is borrowed from another language and behavior is not the same.
Harish Mallipeddi (December 31, 2007 at 3:47 am)
I’ve never read a Javascript/DHTML book until now but if you were to finish this book, it would be the first Javascript book I would buy.
I’ve been reading JQuery code whenever I’m free. I’ve noticed how you make use of some generic patterns including some you have blogged about recently like implementing method overloading in Javascript or avoiding the explicit use of “new” operator to instantiate a class. It would be great if you could identify and document many more of these common patterns in your book.
Marc (December 31, 2007 at 4:22 am)
One thing that’s starting to bother me is the fact that I’m incapable of naming things as I should. Although at first I have no idea what ‘method overloading’ means at second thought I used this way of programming numerous of times, same goes when talking about objects, are they objects or function objects?
This is not something that makes me incapable of doing what I want but communicating with others tend to go like; “I created an object and want to have a constructor like in php5â€, the answer I get is; “What the heck do you mean, start reading a book, you don’t know what you’re talking about!â€.
So a chapter about how things are called would be helpful for me. Love your first book and looking forward to the second one!
Michael Schuerig (December 31, 2007 at 4:26 am)
* How do I use prototypal inheritance for fun and profit? Hey, it’s there, why only use it to fake class-based inheritance?
* How do I minimize memory use of my scripts?
* How do I ensure accessibility despite using JavaScript?
Reggie Drake (December 31, 2007 at 4:54 am)
A few of these gaping holes (anon functions as blocks, timers, portable events) are discussed in http://eloquentjavascript.net . Things like an in-depth look at memory leaks (curse them) and the writing of a compressor (which would also be a good excuse to discuss fundamentally interesting stuff like parsing techniques and the theory of variable scope) would definitely be valuable.
I’ll also second Michael — a good text about JS prototypical inheritance is needed somewhere. Another interesting topic is the mystery of primitive values encapsulated in objects (‘new Number(100)’), since it looks like these will be a confusing case in ECMAScript 4. (Though maybe you are not focusing on ES4 yet.)
Dao (December 31, 2007 at 5:07 am)
Unless it isn’t a typo and it’s the object that doesn’t look like you expect.
see also http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Statements:with#Description
Pete B (December 31, 2007 at 5:22 am)
I’ve recently started making my own mini libary, and finding it quite addictive :) some things I’ve found
non-evil uses of eval, (someone please shoot me down if this is bad) :
mynamespace.local = "for(var i in mynamespace) if (i != local)" +
"eval('var ' + i + '=mynamespace.' + i);"
new function() {
eval(mynamespace.local) // create Closure and local references
// all methods and properties behind 'mylibrary' namespace
// directly accessible
}
Another issue I find a bit esoteric is animation. To me mootools is the best for animation – because it always looks really smooth. Is there some definitive best way to implement dhtml?
Mark (December 31, 2007 at 5:44 am)
That’s great news John, I’ll be looking forward to it.
Maybe you can tackle how it’s possible to make JavaScript easier to debug?
Dougal (December 31, 2007 at 7:27 am)
Well, hurry up and write. I’d like to see this. Email me when its done? ;)
Arjan (December 31, 2007 at 7:34 am)
Maybe how fast an function or method is executed (with different browsers) is an idea, because that is really what the user experiences.
Gavin Brogan (December 31, 2007 at 8:38 am)
I pressume that because your writing a book the snow hasn’t eased where you are.
Howz about? How can chainability be done? (this has really improved my codeing at least)
Throttling function calls(I’m think about events fired by mouse movements and resizing the browser)
also I’m intrested in how other people are caching AJAX requests.
Commenting/Scriptdoc?
Spotting rubbish code?
idioms of library writing?
Some beyond the browser stuff would also be cool. Coding for widgets/Google gadgets?
a nice plot with a happy ending and one or more twist. Maybe kittens for the girls.
Jörn Zaefferer (December 31, 2007 at 8:51 am)
– How to leverage event delegation to create large and dynamic widgets with good performance – my WIP delegate plugin may be interesting there; also, how to achieve that with other libraries?
– How to debug JavaScript? About browser tools like Firebug, MS Script Debugger, etc. or IDE tools like Aptana (I have no idea what it exactly does there)
– DOM metadata – see metadata plugin, Felix’ Active DOM work (jQueryCamp07 presentation), how that relates to eg. microformats
– How does the dimensions plugin (or the offset method) work, and what to do when it doesn’t give me the expected result, eg. how to know when padding/border/margin is or should be included?
There are three modules of jQuery that I didn’t quite “get” yet. Animations and DOM manipulation are already (sort of) on your list, selectors aren’t. Especially those special unicoded regexps are a bit difficult to grasp, good material for this.
Daniel (December 31, 2007 at 8:57 am)
This sounds like a book I will definitely buy.
Tsai (December 31, 2007 at 9:24 am)
I’ve been seeing that (function(){})() snippet of code all over lately, what exactly is that doing?
Satyrius (December 31, 2007 at 9:37 am)
2 Tsai: (function(){})() snippet create anonymous closure and immediately execute it.
masukomi (December 31, 2007 at 9:43 am)
Something no-one ever seems to discuss it books is using javascript for non-html tasks, like using it for more traditional scripting tasks with Rhino.
pd (December 31, 2007 at 9:45 am)
I’d love to read some analysis on the strengths and weaknesses of animation/queing that jQuery and MooTools provides.
How to reduce memory leaks is a great topic I’d jump into as well. However a related topic would be good practices for improving performance with JS, though perhaps that is covered in the first book?
One tpoic that seems to rarely be covered in the discoverability, untuitiveness and accessibility of JavaScript-driven UI. Some rock-solid progressive enhancement/graceful degradation (jargon alert!) patterns for implementing drag and drop along with sliders and various other UI widgets now available through JS needs to be written. Whilst this topic relates to usability as much as coding I don’t see why someone such as yourself John should not have a go at developing such patterns.
As for your aside, I think it’s a super idea. I suggest this should be an online resource rather than in book form though. I suggest a standardised documentation template be drawn up and a site be created to document a variety of code snippets/patterns/tasks. An example could be:
Form validation
– what works
– what doesn’t work
– what degrades performance
– how to improve performance
I suppose I can see a standardised A List Apart-like format but with a collaborative authoring approach so that the painful learning the world’s developers endure can be pooled and eventually reduced :)
masukomi (December 31, 2007 at 9:59 am)
Unit testing with jsunit (both of them) is never covered, and as a result no-one bothers to unit test their JavaScript
Alex Vincent (December 31, 2007 at 10:16 am)
If you need a technical editor, I would probably be willing to.
Kevin Le (December 31, 2007 at 10:20 am)
In my opinion, it shouldn’t be just an explanation of what (function(){ })() does, but when should one choose that over creating a class using function constructor and prototypes.
Douglas Mayle (December 31, 2007 at 11:02 am)
I’d love to see discussion of cross-platform handling of text ranges. In http://trac.openplans.org/openplans/browser/oc-js/trunk/opencore/js/thirdparty/xinha/plugins/InternalLink/internal-link.js#L165 I needed to work with IE textranges like Mozilla TextRanges. I found some broken code on the net designed to add the functionality to IE, and I used the ideas there to create code that actually worked, but my implementation still has some wholes (the act of reading the text range modifies it, and it isn’t restored. After reading, I didn’t need the original TextRange so it wasn’t an issue). It’d be nice to see if there is anything being done with this.
P r o g g a (December 31, 2007 at 11:02 am)
You ended the first book with a 1.5 page explanation of Comet. Is it possible to ellaborate on that on the new book? There are Comet resources online, but I know of no book that dedicates a whole chapter. It took me a whole day to decide which Comet server to use and then learn to use it. I settled for meteor server (meteorserver.org) and when I looked at the JS code it was sending, it seemed worthy of studying.
Paul Huff (December 31, 2007 at 11:18 am)
You know, if there were a quirks-mode-esque wiki with enough eyeballs and mind-share I think it’d get the job done. It seems like if you and a few other javascript folks with the same level of weight in the JS community got together and launched such a wiki it’d probably get a lot of attention at the beginning, probably enough to support it and turn it into the kind of resource that (I wholeheartedly agree) is needed.
cody lindley (December 31, 2007 at 11:30 am)
off the top of my head…
– An entire book could be written about jquery design patterns. This is not how jquery works, but instead how jquery is coded and why. The why is very important! Dustin Diaz took a stab at this and its excellent. We need more people talking about they whys, and less about how to use an API, custom or core. Patterns solve problems; we need more books telling us why a certain pattern solves a particular problem.
– Real world event Gotchas! Lets stop explaining events, and browser specific event models. Instead, I want to see 10 examples of event gotchas that occur in web applications.
– A good chapter explaining the object in regards to the core language and how it’s the bases for everything in javascript. I’m tired of stupid chapters that show me how to create a generic object, and then an object that might have some OOP characteristics. What readers need is context! For example:
— What is the global object, and why is it important.
— Why is everything in javasccipt an object. How does this effect custom objects and core objects.
– The future, we need a rational explanations as to why javascript is moving in the same direction as actionscript. That is to say, it’s looking like javascript is moving towards java, just like actionscript did. Personally this scares me a bit, but I have yet to read a rational explanation as to why this is necessary for the majaority of people using javascript. Changing it to an advanced OOP language just so a small percent of advanced developers can code more advance patterns seem like silly logic.
– Several examples of closures, not just one or two sprinkled here or there but an entire chapter of 10 different uses for closures.
– You first book shows a lot of logic that obviously was the ground work for jquery. I’d love to see you tie the ground work closer to the actual library.
– A chapter on how the YUI compressor works and maybe an example using a custom ant script would be great.
– Anything more on jsdoc’s and jsunit would be great.
– In general more about objects. You did this in you first book, but I wanted more. I wanted deeper explanations and more examples. Objects and how they are pragmatically used in really world code.
Joshua E Cook (December 31, 2007 at 11:43 am)
There is a real need for a good explanation of how to do prototypal inheritance properly. I’ve seen myriad ways of simulating classical inheritance, all of which require too much extra scaffolding for my taste.
This single function gives you just about everything you need to effectively work with prototypal inheritance.
function object( proto )
{
proto = proto || {};
var T = function () {};
T.prototype = proto;
return new T();
}
Frank Thuerigen (December 31, 2007 at 11:50 am)
As I see it (and we discussed related issues before John) one of the most challenging changes in common JS usage is the use of the module pattern. I see a leap in code precision in application programming lately, as opposed to former use of JS.
However, as the code gets more complicated, some people overdo it. I think one of the most urgent problems to adress nowadays is defining what patterns you SHOULD use to precisely define application functionality, and most of all what NOT TO DO in order to keep the code maintainable and debuggable.
Also I´d like to see a chapter about asynchronous JS in conjunction with estimating and controlling CPU load in JS.
Also so far there is a set 10-15 do-and-don´t rules in JS programming. Nobody ever split that up into lib coding and app coding. So I´d like to see a little paragraph about the differences between application and library coding.
There also should be a discussion about the different challenges that apply to server-side FWs + AJAX as opposed to FullAJAX coding. This would probably lead to a definition which type of structure you need for what is demanded. There defiitely is a difference between those, especially in memleak fixing issues.
One of the most challenging parts is BALANCING a JS app since you have to deal with limited resources. For example: animations etc. have so far only be discussed from a user experience point of view. There should also be a discussion about it from a “CPU load” point of view.
Summing it up: in my eyes the MODULE PATTERN and the BALANCING are no1 priorities here.
BTW I hereby order a book signed by the author ;-)
Have fun, John,
…and a happy new year :-)
Eddie Welker (December 31, 2007 at 11:58 am)
Seems like a great chance to write a really useful book. I make it a point to read as many as possible, and think they are too similar.
I agree that a nice section on prototypal inheritance would be good. The literature is particularly sparse in this area. I’d like to see exploration of features like currying and functional programming. How they’re accomplished in js, and why they are so useful.
For your aside, I think that the idea is excellent. But if you were to frame it in a “how-to” method, you wouldn’t have to go over every browser, or keep it updated. That could be left up to the reader. That sounds like a great addition.
Karl Swedberg (December 31, 2007 at 12:07 pm)
Hey John,
I don’t have any particular suggestions that haven’t been mentioned already, but I do want to say that I think this idea of gathering requests for book content is brilliant. If more writers did this kind of research before authoring their books, we’d all have much more useful reference shelves. Cheers!
Brennan Stehling (December 31, 2007 at 12:27 pm)
– Unit Testing for visual interactions (drag&drop)
– Doc Comments for IDE support (Visual Studio has basic support now and I expect there will be richer support in all IDEs soon. The emerging standard looks promising
– Debugging techniques for all kinds of scenarios (ajax callbacks, setTimeout, etc)
– Smooth animations (perhaps by the time the book is written you can cover FF3, IE8 and Safari which appear to be focusing on this sort of performance)
– Class management (like maintaining the context for “this” consistently)
Doc Comments
http://weblogs.asp.net/bleroy/archive/2007/04/23/the-format-for-javascript-doc-comments.aspx
jQuery Stub for doc comments
http://www.smallsharptools.com/Kvetch/scripts/jquery.intellisense.js
Sasha Sklar (December 31, 2007 at 1:06 pm)
John,
What’s the difference between “(function(){ })()” and “(function(){ }())”?
I’d like to see a set of larger application design patterns at the beginning of a book like that, how to organize the entire application basically.
Peter Goodman (December 31, 2007 at 2:32 pm)
In line with Stefen, I think a good look at ‘prototype’, ‘__proto__’, and direct setting to javascript objects affects an object and what the use cases / (dis)advantages to each are.
Dean Edwards (December 31, 2007 at 3:41 pm)
> How do I write my own JavaScript compressor (like Packer)?
I’d be happy to help with that if you want.
Juha Suni (December 31, 2007 at 3:58 pm)
I would like these topics given more exposure:
– How to use event delegation to simplyfy code, enhance performance and make things easier (no rebinding on dynamically added DOM-elements).
– How and when to use custom events and trigger them, what are the benefits?
Caustic Dave (December 31, 2007 at 4:14 pm)
1) Memory Leak detection.
2) Debugging tips.
3) The secrets you will withhold but put into your third Javascript book . :-)
Travis (December 31, 2007 at 4:16 pm)
How about basic stuff like when to use == vs ===? Or JavaScript’s truthiness expressions?
Timers and memory leak info would definitely be appreciated.
Kris Kowal (December 31, 2007 at 5:48 pm)
@Peter B: or, you could use a |with| block for an O(1) solution ;-) but that would depend on whether you find |eval| more or less “evil”. You would also want to put an enclosure inside the |with| block to avoid some quirks, or just put your |with| block and enclosure around all of the functions that use your namespace.
|with| is a power-tool. As the adage about the C programming language goes, you might not want to use a chain-saw to cut a cake since it would just as happily cut the table and probably your leg, but that doesn’t make it /less/ useful.
ziad (December 31, 2007 at 7:10 pm)
I think a discussion on how to properly document js code would be interesting
Scott Schiller (December 31, 2007 at 10:23 pm)
Javascript animation is a common question, I’ve written about what I’ve learned previously on my site (mind you, it’s fairly basic – I didn’t get into skipping/dropping frames and so on.)
I second event delegation, and also would suggest something along the lines of “event throttling” (eg. limiting execution of expensive event handlers on mousemove() or window.resize under IE, etc.)
ram shrivastav (December 31, 2007 at 11:50 pm)
Core Javascript
– stack traces in javascript
– lexical scoping of javascript ({} does not start a scope)
– unicode variable names in javascript
– end of line, semicolons optional in javascript
– interesting side effects and bugs
– undefined variable and undefined types
– use of void operator.
– call and apply methods
– function currying
– duck typing
– interfaces design
– client side mvc model
– greedy and reluctant quantifiers
– positive and negative lookahead in regexps in js
– regression testing
– automating js/webpage testing using selenium
– minification
– obfuscation
– compression
– jsdoc
Nathan (January 1, 2008 at 1:11 am)
Closures. Still don’t get ’em.
Eduardo (January 1, 2008 at 2:05 am)
I would like to ask about javascript/ajax streaming… instead of having a heartbeat function, one that could read from the server a stream, and parse data(like a sax xml parser)
thanks
eduardo
Filipe Campos (January 1, 2008 at 10:06 am)
Hi,
I’ve read your first book and it was great, simple words that programmer understand.
My criticism of the book was really that, the real advanced stuff, and the pictures. Too many pictures and great ones are not need in the book.
But great book as I said, it’s worth the money.
FIlipe
Michael (January 1, 2008 at 2:01 pm)
“There needs to be volumes written on how to fix real-world JavaScript bugs, constantly being updated to handle the changing browser landscape….”
I’m with Paul, I would absolutely love to see a wiki dedicated to this!
Dustin Diaz (January 1, 2008 at 4:28 pm)
John, I think the book you’re new book will make a good companion to the one Ross and I just recently wrote. The reason I wanted to write about Design Patterns is because they are solutions in a different context, that in one sense, has never been touched before in JavaScript literature.
On another note, I too am a bit exhausted by the heavy dosage of DOM and Ajax hoopla’s these days. Everything seems to be a cook book with the same mundane examples that can already be found on websites.
Anyhow, good luck with the writing. It’s awesome you’ve started to dive into a second book :)
Dethe Elza (January 1, 2008 at 7:38 pm)
Every library has to work around the same set of browser bugs to provide a common API. I’d love to see a canonical list of these bugs with example work-arounds that don’t depend on committing to a specific library.
Even better would be if libraries like jQuery, MochiKit, Dojo, et. al., could agree on a format to document the bugs they work around, kind of like how OS X documents its frameworks in such a way that Python, Ruby, and Perl can all share the same definitions and autogenerate wrapper code.
It’s a new year, I can dream a little! Looking forward to your book.
Phil Palmieri (January 1, 2008 at 10:07 pm)
Hey John,
Few things… First, in your book – (dunno the publisher’s take on this) maybe add “Question from John Doe” ? would be cool.
Something that comes up a lot, especially with YUI chats and all.. is explaining in detail, the benefit/drawbacks o using auto instantiated objects.. foo = function(){}();
Then for myself, how bout a luff-less walt through of custom events and handlers. – with or preferably without outside libraries.
Phil
pd (January 1, 2008 at 10:59 pm)
FWIW my suggestion is probably most easily summarised as the wiki that’s been suggested. I just didn’t want to use the cliche wiki concept as a limiting factor on the idea. However sure if that’s the easiest thing to get started, let’s go!
Blair McBride (January 1, 2008 at 11:44 pm)
I’d like to see sections on accessibility and secure coding with Javascript. These two topics are so important, yet so overlooked in general Javascript development.
And with Javascript applications getting as large and complex as they are, a section on performance-enhancing tweaks – how do I optimize my Javascript?
Blair McBride (January 1, 2008 at 11:48 pm)
Just thought of something else – what are the ways of implementing undo/redo in Javascript/AJAX applications? This is becoming very important as we see more and more web apps become come closer to desktop apps.
Vikas (January 2, 2008 at 1:06 am)
Seems like these are good interview questions for Frontend Engineer.
Here, How do I write a cross browser way of….
Is still challenging for me without any framework.
Jordan Sissel (January 2, 2008 at 3:54 am)
John,
Regarding your aside about expanding on what quirksmode has partially solved – Are you thinking some kind of centralized location online (a wiki or similar device) for this kind of information? With the language changing so often it seems like quirks, bugs, and workarounds are a poor fit for print.
Jakub Nesetril (January 2, 2008 at 5:18 am)
With regards to timers, it would be good to write about threading etc. Just a couple of weeks ago, we spent considerable energy finding information on what is blocking and what is not, how exactly asynchronicity works, how is it inlined back into the main thread, wether timers create new threads etc. Also mentioning WorkerPool and other new developments could be useful.
Moran Ben-David (January 2, 2008 at 8:55 am)
I’d like to see something explaining the timing and events used to communicate between the client and the server. A good example of this is gmail in the way it saves and loads state efficiently.
If one has an application that is part single document (ajax) and part multi-document the session issue becomes a problem when attaching to events like onbeforeunload (since it gets fired when clicking on anchors with href=”javascript:…”! in IE).
Frederick Polgardy (January 2, 2008 at 9:27 am)
Firstly, hats off for jQuery! I love it; I spent about two weeks of train commutes just reading the source.
I’ll toss my hat in with the prototypal inheritance folks. I’m starting to grok it myself, but wrapping your head around it is key to getting around in JS.
How to structure JS libraries: module patterns, class/inheritance patterns. (Which is precisely what (function(){})() is for.)
DOM event model.
Designing front-end code for testability. I’m trying to really gather my thoughts together on this topic at the moment. Would love to get your thoughts.
Matt (January 2, 2008 at 10:12 am)
Something I would love to see that I have never been able to find a good site on explaining is how to properly handle displaying a web-app at multiple resolutions. This sounds more like a CSS issue but I’ve never been able to get CSS to handle things properly. Looking at various Google applications, it appears they employ some form of dynamic rendering, undoubtedly utilizing Javascript.
Angel (January 2, 2008 at 10:30 am)
I’m very interested in how to make good use of the browser window. So it would be nice to see a good discussion about the viewport, how to handle window resizing, … Maybe give an example of panels that can layout elements differently.
+1 to a wiki (“jspedia” or something like that…). With John’s visibility it would be a sure hit and very useful.
Marty Stake (January 2, 2008 at 1:31 pm)
I second Cody’s “We need more people talking about they whys, and less about how to use an API, custom or core. Patterns solve problems; we need more books telling us why a certain pattern solves a particular problem.”
– specifically – I’d like to see the whys/hows of handling user interaction with widgets (tabbed or carousel) which allow hover or click events and show change with animation – if a user clicks real fast, or hovers over all the options quickly – how to cancel the running one and move to the next without breaking the animation sequence. Queuing patterns, I guess.
NICCAI (January 2, 2008 at 1:56 pm)
I’d like to see more on ranges – lots more.
And, this may seem funny, but I’d love to see more on migrating to and from a custom library to jQuery (or other) – and vice versa. For large teams, it can be both necessary and difficult to transition to a library from custom code. I’d like to see more on managing library and application bloat in the real world – with particular attention to final client side performance. Also, your perspective on deployment would be appreciated.
Myk Melez (January 2, 2008 at 3:12 pm)
Several folks have mentioned a section on performance, and I agree. A section on profiling JS would be killer.
Michael Mahemoff (January 3, 2008 at 8:27 am)
Real-time graphics, e.g. data: URIs
Gijs (January 3, 2008 at 9:11 am)
So yeah, off the top of my head:
– Profiling with Venkman/Firebug/Safari/IE debugger (do the last two do anything? Not sure…)
– Prototypes and the variable nature of |this| (coupled with |apply| and |call|) and “static” properties (function Foo(){}; Foo.prototype.bar = 1; Foo.bar = 2;)
“Anonymous functions considered harmful” – this is a bit technical and low-level, but for debugging and readability, it would be good to explain that:
– whenever you use function(){}, you can also use function foo(){}
– function foo(){} is not the same as var foo = function(){}
(ie. functions have their own names which are given when you create them. Debuggers need those and the interpreter doesn’t care (in spidermonkey, at least) that you assign the function to a variable named X, leading to problems such as https://bugzilla.mozilla.org/show_bug.cgi?id=347888 )
Finally, the setTimout(fun, 0) trick (“do this after layout finishes”), as well as the fact that the last argument passed to fun will be the offset in ms. by which the timer ‘missed’ its assigned firing time.
William from Lagos (January 3, 2008 at 10:35 am)
How about you write more on Namespacing. Pro Javascript Techniques lacked extensive information on this.
Frank Thuerigen (January 3, 2008 at 10:42 am)
@those who cannot wait:
the concepts the “module pattern” is based on are presented here:
http://blog.phpbuero.de/?p=19#more-19
This includes abstract functions, var scope in general, the block scope pattern, closures and a sample of how to create an application object using the module pattern.
HF@all Frankie
William from Lagos (January 3, 2008 at 11:23 am)
Hey John will you be covering stuff on Javascript 2.0?
When is it due anyway?
Srinivas Grandhi (January 3, 2008 at 12:10 pm)
It would be good to describe a consistent way of cross-browser debugging of javascripts. For example, alert() works well in IE, but does not in Opera.
I am eagerly waiting for your book.
Martin Hassman (January 3, 2008 at 6:03 pm)
I would like something like “Most common ways how to make your script quicker”. You, John, have from jQuery probably enough experience with optimization for performance.
Davide Ficano (January 4, 2008 at 10:52 am)
It would useful to see the different ways to define Singletons, show patterns to write code simple to debug and to maintain.
My two cents
Matt K (January 4, 2008 at 11:18 am)
I haven’t seen too much about using Design Patterns (e.g. Observer/Observable, etc) in JS. Another topic, mentioned already, would be how to mimic threading. I think people might also be eager to learn more about what’s coming in JS4.
Loved your first book, can’t wait for this.
Andrew (January 6, 2008 at 4:54 pm)
Perhaps you could cover
new function () { ... }
and contrast it to both(function () { ... })()
and object literals and when someone would choose to use which, it is something I have found myself explaining to people new to real JS development a lot.Techniques for lazy code execution and evaluation, async programming for pseudo-concurrency, explicit continuations, and other related programming techniques for developing applications that do a lot of intensive processing on the client and avoid ui lockouts.
Tom (January 7, 2008 at 11:26 am)
I personally very much prefer new function() {…} to (function() {…})(). It’s a bit of an abuse, but it’s much easier to read (syntactically) and write. And when you actually use the ‘this’, it’s not quite as much of an abuse. But I abuse it if needed anyway.
Tom (January 7, 2008 at 11:27 am)
And don’t forget to mention what’s wrong with ‘with’ if you cover it.
K9 (January 7, 2008 at 2:22 pm)
I agree that a discussion of the threaded-ness of javascript in the browser would be great. I think most developers know that javascript is a single threaded, but then when you’ve got event-driven apps that are doing animation and accepting user input “at the same time” the lines get blurred.
When you consider that up until multi-core and hyperthreading came about all threading on desktops was in a sense an illusion (i.e. everything is essentially serial on the processor) the lines get blurred even further. Anyways I’m no expert on this but a deeper discussion of this would be cool.
Tom (January 7, 2008 at 5:47 pm)
How about sneaky non-jittery scrolling for margin/gutter areas? Sometimes “position: fixed” isn’t good enough or just isn’t available (IE). I’ve learned at least one trick for this, but based on the amount of jittery things out there tracking scroll events, I’m guessing the tricks aren’t well known.
Thomas (January 8, 2008 at 6:41 am)
Contentswise, I’d love to see something about Object Pooling. Object creation and destruction is expensive in most browsers, and big apps would benefit from object re-use.
Processwise, after collecting ideas for the new book you might find it interesting to continuously share your book progress with a community, e.g. by publishing (restricted) online versions of chapters, to get early readers and their feedback. I once was on such a book list (http://hop.perl.plover.com/) and it was exciting and gratifying to take part in the evolution of a book.
Toes (January 9, 2008 at 1:24 am)
What I’d like to see is an updated discussion of current thinking on JavaScript Object-Oriented programming. I know you covered it in your first book, but now we have Dean Edwards’s Base2. And Douglas Crockford has given us this (http://javascript.crockford.com/prototypal.html) but without any examples.
Andrew (January 9, 2008 at 3:34 pm)
@Toes, wrt prototypal programming in JS, you might be interested in reading Crockford’s chapter in the book Beautiful Code, where he uses prototypal JavaScript to develop a parser for the subset of JavaScript he recommends. I know its not as accessible as possible, but its the best I’ve seen
J-P Stacey (January 10, 2008 at 9:35 am)
I’d like to see a Javascript book where simple data types like strings and numbers are relegated to an appendix, rather than being the first thing you see. I’m a bit sick of having precedence of arithmetical operators explained to me in each language before I’m taught the bits that make it different from every other language.
In this ideal book I’m imagining the first few chapters would be about (roughly in order): objects, prototyping, closures, command-line implementations like venkman, and events/asynchronicity/single threading (including a discussion of the hypothetical browser), before going onto more specific topics like compression, performance, or patterns.
I’d avoid discussion of specific browser bugs or memory leaks, because they would eventually date the book: given time, many of these problems will hopefully be solved. Perhaps a passing reference to garbage collection, and another appendix on bugs, might suffice?
Morris (January 13, 2008 at 7:54 pm)
It seems such a shame to write a non-interactive book about Javascript when it is so easy to add good live Javascript examples dynamically in the browser.
I wrote an article on closures and another on how prototypes work (blog.morrisjohns.com) with live examples that help explain the concepts much more clearly than the words do.
I think that most people learn better by playing with real examples than they do from reading dry textual explanations.
Hal Helms (January 15, 2008 at 7:34 am)
Agree about how valuable a companion website would be. From interactivity to videos to forums, this would be a godsend. Personally, I’d be happy to pay for access to such a site.
Kaiko Kaur (March 20, 2008 at 5:09 pm)
1. I think good explanation about variables is needed. For me it was confusing that every variable is reference to object and if I assign new value to variable the new object is created without changing old object.
It is different about other programming languages (like php).
For example
function chobj1(o) { o.x = 1; }
function chobj2(o) { o = [1,2,3]; }
myObj = {a:1}; chobj1(x); // x ==> {a:1, x: 1}
myObj = {a:1}; chboj2(x); // x ==> {a:1}
a = [1,2,3];
b = a; // it is not copy of a
a.push(4); // b will be also [1,2,3,4]
a[3]++; // a and b are both [1,2,3,5]
b = [3,2,1] // a is still [1,2,3,5]
a = 5;
b = a; // deeply inside JS, b is reference to a, isn't it!?
a++; // why now b is still 5
What to do if I want to write function that modifies variables that I pass with arguments? It is usual technic in another languages after all…
2. How to get useful javascript errors on working environment? It is quite useful to use something like…
onerror = function(msg, url, linenr){
var s = new XMLHttpRequest();
s.open('POST', './sendmail', true);
url = encodeURIComponent(url);
msg = encodeURIComponent(msg);
s.send('url=' + url + '&msg=' + msg + '&linenr=' + linenr);
}
Is there possible way to get more and better information than just “hey… here is some problem :)”. Is there way to get some variable dump and call stack?
3. This is maybe too small question, but anyway…
Every variable is actually some object property. So…
var a = 'foo';
alert(window.a); // same as alert(a);
if (!window.b) alert("b doesn't exists");
if (!b) alert("b doesn't exists"); // generates error -- WHY?
Hansor (June 10, 2008 at 2:40 am)
Hi,
I would be very interessted in thoughts about solving the namespace problem. e.g. How to solve the problems, with using multiple framwork libraries like Prototype and MoochiKit in one project
sswong (June 10, 2008 at 9:33 pm)
Does anyone know what is this
(function(){…})();
call?
because whenever i try to explain this to friends or colleague, i just don’t know how to call/name it.