Raphael.js event handlers sometimes require closures

I’m playing around with raphael. In the code below, I use a few loops to make a grid of rectangles. I want each rectangle turn from white to gray when clicked. This is a little tricky in raphael, because you can’t use this or evt.target to get a reference to what got clicked on.
var main = function () {
var paper = Raphael("grid", 320, 200);
for (var rownum=0; rownum<3; rownum+=1) { for (var colnum=0; colnum<3; colnum+=1) { var r = paper.rect(10+colnum*50, 10+rownum*50, 50, 50); r.attr({stroke:"gray", fill:"white"}); r.node.onclick = function () { var this_rect = r; return function () { this_rect.attr({fill:"gray"}); } }(); } } }; window.onload = main;

Remember that your typical boring onclick assignments usually look like this:
r.node.onclick = function () {
alert("your mom");

I don't call the function. I just define it.

Study the r.node.onclick assignment in the main code again, and make sure to notice that the r.node.onclick attribute is assigned to the result of the function defined.

That's the first big difference. Now you have to figure what the heck is returned by this function. The answer is. . . another function. That other function is what gets linked up to be fired when a click event happens.

Closures are tricky at first, but they're really useful for situations like this, when you need to pass along references with the functions to operate on them.

How I cook kale

I’ve watched a lot of kale recipe videos on youtube, and learned a few neat tricks there.

I’ve tried it raw, and I just don’t like it that much. Cooking it may destroy some of the vitamins, but it doesn’t destroy all of them, and I need food to taste good or I won’t eat it.

I bet a really great chef can make raw kale taste delicious.

Anyhow, I’ve been cooking the kale lately by picking the leaves, washing them, stacking them up, rolling them into a tube, and then chopping up the tube. People call this “chiffonade”. You can watch videos on youtube to see how it works. It’s just a fast way of chopping leaves.

Then I throw some butter and olive oil into a skillet on medium heat. And I add some chopped onion and garlic. If I have any crushed red pepper flakes (like from pizza delivery) I’ll throw those in there. I push the onions and garlic around in the oil and butter with a wooden spoon to keep them from burning for about five minutes. The onions will turn a little clear, and the garlic will get golden brown.

At that point, I throw in the chopped up kale. The left-over water on the leaves will steam. Then I just keep stirring it all together.

You can take the kale out whenever you want, so experiment with different cooking times. I like it when it is totally wilted, but before it starts getting brown.

Some times, I’ll add a little more water (like about 1/4 cup) and then put a lid on it to make the kale steam.

Usually I eat kale with a fried egg and toast. Once the kale is nearly done, I’ll push it to one side of the skillet and then crack an egg in the other side and put in a slice of bread in the toaster.

Then that all ends up in a big bowl together, maybe with a few shots of hot sauce on top if I didn’t already add the red pepper flakes. That makes a good breakfast.

The onions get sweet when you cook them, and the combined taste of the onions, the kale, and the pepper or hot sauce really appeals to me.

I’ve experimented with cooking the kale in sesame oil and adding soy sauce, but never really felt like I got it right.

If you really get into kale, you might try growing it. I eat it all so much because it grows so fast. I grow the red Russian variety from seed I purchased at Baker Creek.