JavaScript: Private Static Members, Part 2

Finally, it’s time to finish up the lesson on private static members and methods in JavaScript.

Last time, I introduced the technique of creating and immediately executing a function, using parentheses. I talked a little about returning a function and storing it in a variable.

var myFunc = (function () {
  return function () {
    alert("Hello, World!");
  }
})();

alert(myFunc); // "function () … "

myFunc(); // Hello, World!

(more…)

JavaScript: Private Static Members, Part 1

A little while ago I talked about creating private variables and methods in JavaScript. This works, but is not necessarily efficient: each instance of the class creates new copies of the members. While that may be exactly what you want for instance variables (think of partNum in the old examples) it is not always ideal.

The complexity jumps significantly, though. So I’m dividing this half into two parts.

To get started, we need to forget about all this Object-Oriented Programming for a minute and look at some of the neat tricks you can do with functions in JavaScript.

Update: Part 2 is now available. (more…)

Firefox: Open in Blank Tab

If you don’t use Firefox 3, go get it. Then finish this article. (Safari and Opera users are excused, but there’s no promise this will work for them.)

One of my (few) gripes with Firefox is that bookmarks on the toolbar have no “open in blank tab” option. They have an “open in sidebar” option, but those uses are rare and esoteric at best. Personally, I never use the sidebar.

“Open in blank tab” should basically do this: if there is a blank tab, use it; if not, create a new tab. Frankly, it could just open in a new tab regardless, but it seems like such an easy thing to add.

But? It can’t be done directly in Firefox. Hence, I present this small script:

javascript:
(function(){var u=‘http://mail.google.com/mail’;
  if(window.location==‘about:blank’){
    window.location=u;
  }else{
    window.open(u,);
  }
)();

That’s it. Try dragging this link to GMail to your bookmark toolbar. Then click the link on your toolbar. Now, open a new tab, and click the link again.

This isn’t exactly what I asked for. It has no way of knowing if any blank tab exists, only if the current tab is blank. And, of course, it lacks the nice favicon support.

But it does the job. If you change the variable u to something other than ‘http://mail.google.com/mail’, you can make the link open any other page.

I love anonymous functions.

Update: If you want a bookmark for something besides GMail, you can create your own. Or you can drag this link to your toolbar, to make new ones whenever you want: Open in Blank Tab.

Update 2: Oops, fixed the “create your own” link. Tested it, then accidentally pasted in the results, instead of the actual script.

Private Variables in JavaScript

Ok, enough of this social/ranting stuff. Time to write something vaguely technical.

I have a love-hate relationship with JavaScript. I think anyone who works with it does. Sometimes it just doesn’t do what you expect, and it’s certainly different.

One trick, especially for people from real Object-Oriented languages like Java, Ruby, or let’s even say PHP 5, is the lack of access control. When everything is an object, the inability to hide certain values can become a problem. (more…)

Chrome Is Not A Browser

If you somehow haven’t heard of it, Google’s Chrome is a neat, quick, Acid2-compliant “browser” designed to work with web applications, not web pages.

Chrome certainly looks like a modern browser, with tabs along the top and an address bar and a “Most visited” home screen, it will seem familiar to anyone who’s moved past Internet Explorer 6.

And yet, my Twittersphere has been full of comments like “Nice, but not nice enough to make me drop Firefox/Safari.”

While there are some visual improvements, such as an extremely small “chrome” (the parts of the browser around the page area) footprint, the big changes are “under the hood.” Chrome is built for tabs—each tab is an isolated process; no one tab can take down the whole browser—and is built for JavaScript-heavy “web 2.0″ apps—Chrome’s new V8 JavaScript engine executes a full order of magnitude faster than the current browsers, in my experience.

And all of those “under the hood” changes are open source.

Chrome is not a browser.

Chrome is Google’s way of making a point: modern web browsers have not kept up with the web itself.

More and more, the web is becoming an interactive application, and most browsers are not built for it. They display pages, and running applications is an afterthought. While we’ve seen huge improvements in JavaScript execution in the past few years, speed is still a limitation for developers. Applications are also much more likely to crash than static pages (go ahead, just try to crash a browser with just malformed HTML) and isolating tabs will give necessary boosts to speed, stability, and security.

Kris Abel of CTV.ca said it best: “Google’s entire business takes place throughout the internet itself and so they see their interests served regardless of which company takes web browsing to the next level, in fact they see their interests served if all companies do exactly that.”

I’m not switching to Chrome. I doubt very many people will find it useful as a primary browser. I don’t expect many user-interface improvements, like Firefox’s vast add-on library or the accessibility features of Firefox 3, Opera or IE8.

I do expect any future version to have more “under the hood” improvements, and I hope that the makers of Firefox, Opera, Internet Explorer, and any new browsers that spring from this, will re-evaluate their own products and move in this direction.

Because when the browsers get better, the web gets better.