The new Apple iPod Nano is missing an internal gyroscope (or 3) to know which way is up. Never fear: it’s a feature. Steve Jobs demoed the device this week and was saying how cool it was you could use your hands to rotate the display. They should have used the ‘3D’ technology from the Nintendo 3DS to make the display render in all 4 directions.
Author Archives: Randy Peterman
Not Asleep So Much
I’m awake, but I’m tired.
I’m tired, but I’m allergic.
I’m allergic, but the air is not going to get any less pollenated.
Pollenated air doesn’t produce air seeds because it turns out air doesn’t reproduce [that I know of].
I’ll stop there.
La Bamba
Tonight we had a good old fashioned jam session at the Kaes house. It was an all request night by which I mean anyone could pick a song (I didn’t really pick a song, but I did try out a new guitar/melody/song piece accompanied by hand percussion and it was quite cool). It was in honor of a friend, Eliska, who is from the Czech Republic, but was visiting for a number of weeks over the summer but has to fly home Tuesday. She plays quite a few instruments so it was good fun. We also had “Funkle Doug” on the piano (for some of the time), who is a great jazz pianist. Eventually we worked our way to the song La Bamba.
I’m not sure how it came about that we played La Bamba, but we did. There was dancing. Young and old were either playing or dancing. It was good. There was life, there was fun, there was family, friends, and fellowship. There was love. All from a silly 3 chord song with a catchy hook. I hope your weekend had a La Bamba in it.
JavaScript/ECMAScript Closures Up Close and Personal
One of the most important tricks an ECMAScript coder can learn is how to properly create ‘object’ with prototypes. A second-place runner up is the use of closures. Closures come from JavaScript’s secret roots in Scheme. They’re a technique for letting scope ‘linger’ around for functions that are called within a function. I looked for tutorials on closures but couldn’t find any immediately that didn’t basically alert someone’s name, which just doesn’t seem very practical to me. Alerting your name is the boring cousin of “Hello, World.” So let’s look at a practical problem with a need for a solution. Let’s say that you have a prototypical object that happens to enhance HTML links that the user can interact with. Since you’re doing progressive enhancements the link will work as expected on browsers that don’t have JavaScript enabled, but for those spiffy browsers that are progressive (not a political statement), you’re going to attach an onClick event. I’m lazy so the examples below are going to use jQuery event attachment, but longer-form addEventListener, which is wonderful and will make your life more fulfilling.
So when you click on this link you want it to be able to refer to your object so that you can work nicely with other libraries or have multiple instances of the object on the page. The hacky ways to do this might have included adding attributes to the HTML object, doing some sort of global object lookup, or any number of other things I did when I didn’t understand closures. But here’s what you’re going to do:
<html>
<head>
<title>Never Gonna Give You Up, Closures!</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
function MakeItCloserWithClosures()
{
/**
Assigning a variable 'that' to have the value of 'this' creates a reference that maintains scope through closure, so sub-functions/lambdas, can reference that as a variable that is in scope to call functions withint this object
*/
var that = this;
$('.makeMeClosureFriendly').click(function(){that.rickRollTheUser();return false;});
}
MakeItCloserWithClosures.prototype.rickRollTheUser = function(){
document.location.href = 'http://www.youtube.com/watch?v=3HrSN7176XI';
};
/* use jQuery's ready function to initialize the object, which should attach it to the HTML objects. */
$(document).ready(function(){ new MakeItCloserWithClosures(); });
</script>
</head>
<body>
<a class="makeMeClosureFriendly" href="http://www.digg.com/closures-are-easy">
Closures are easy!
</a>
<body>
<html>
There are a number of possible issues with the above code as far as scope is concerned: 1) the new object is not assigned to a variable for future reference (which we’ll pretend is awesome and the best idea ever), and 2) since it isn’t assigned there’s no global variable that we could attach to inside of the click event handler (function). Except that with closures we can use the code’s reference to ‘that’ and it will refer back to the originating object and Rick Roll the user. Closures are the best for handling references back to an object that needs to touch code, but not be global in scope or concern itself with as much interaction with other libraries.
I like to think of closures as ‘scope tunnels’ that can linger on for some time as long as the objects that reference them still exist. In complex web applications being able to reference back to a parent object can save lines of code, confusion and can help call functions that are aware of other variables, details, and settings that exist within a JavaScript/ECMAScript object. Fewer lines of code to debug is a big win, and fewer lines of code to maintain is even better. I hope this helps!
Where will you use closures today? See the demo to get Rick Rolled. Please leave questions in the comments.
Web Workers: No JQuery
At present JQuery, the popular library for web development, is not capable of sitting inside of a Web Worker instance. It probably isn’t a huge deal for most folks, but there might be cases where the JQuery syntax or utility functions would have been nice. AJAX can still be handled in the traditional ways (and all browsers supporting Web Workers can use the proper JavaScript object and not the old IE Active-X hack). And since it’s basically a background thread you can even use synchronous AJAX 😉
JQuery may get a new implementation or a sub-library that can handle being in a thread, but for now, don’t count on it inside web workers.
Abby and Evie in Colorado Springs
Abby and Evie in Colorado Springs
Originally uploaded by RandyPeterman.
We went to Colorado Springs today for a pre-Father’s Day family time. The girls were cute at the zoo there.
2000
WordPress is telling me this is my 2000th post. Not a lot of content here, but just for grins:
1) The surprise non-geek post: Ligers
2) I’ve been blogging (mostly at this site, but when I started on a site that no longer exists) for over 10 years
3) Doing the math on that, I’m not a very consistent blogger
4) Before switching to WordPress I wrote my own blogging platform
5) Most people really want pictures of my daughters on this site, but I’m a bit short on photographs
6) After all this time I think my Sister-in-Law, Shari, still thinks I’m a dork
7) I have probably gotten the most comments on this blog from my mom, who used to regularly correct my spelling and grammar
8) This list isn’t very interesting
9) The blog I used to link to, but that is defunct, but that I miss the most is “Apropos of Nothing”
10) Before Facebook and Twitter I used to spend a LOT more time on this site
All Smiles
Thanks for the Fish
When I used to work at the Christian book store in college I was a new hire and the owner was showing another new hire how he expected a display rack cleaned. He called it the “auto center.” I laughed because I thought it was a clever name. It was a name that sounded more grand than the plastic display actually was.
I got called into the owner’s office and was scolded for making fun of the name by laughing. I would like to take this time to point out that you’re welcome to laugh at all my posts – whether they’re intended to be funny or not – and I will not call you into my disorganized office (which I will be cleaning this week). I will, however, show you my guitar center.
Melborp A Evah I
I have a problem: every time I run into a foreign name that is spelled out that is unfamiliar to me I read it backwards just to make sure someone isn’t playing a joke on me. I’ve read too many books, watched too many movies or something that causes me to do this. So far I have found zero actual cases of foreign names that are funny backwards… but I’m watching… waiting… I will not be fooled!