chenb•log

I don’t mind if I’m incomprehensible

Archive for October 2007

The Best Agrees

with one comment

Maddox sez there’s a connection between crocs and NASCAR. There must be, since he’s always right.

I slightly disagree with him on babydoll tops. (Is that what they are called? Why do I even know this shit?) They look okay on girls who are a little chubby. And by the way ladies, it’s okay to be a little chubby, depending on your body type.

Maddox didn’t rant about ugg boots and big ass sunglasses though. He must be dating someone who wears them simultaneously. It’s fucking tragic when a really cute girl does that. TRAGIC!

Written by Barry

October 29, 2007 at 9:33 pm

Posted in General

JavaScript "with" Cheesy Hack

leave a comment »

code, pre { font-size: 1.3em; } The quotation marks around with are not unnecessary because this is about the JavaScript keyword.

Douglas Crockford said in his “The JavaScript Programming Language” video lecture not to use with, because the scope gets all confused. Or something. But since I’m not a senior JavaScript Architect at Yahoo!, I can cheesy hack all I want.

Here is the relevant code:

with(this) {
  GEvent.addListener(polygon,
                     'click',
                     function(point) { clicked(OMap.getMap(), point); }
  );
}

That code is in a public class method (drawPolygon). I need with for the clicked method used in the anonymous function for handling the click event.

Without with, clicked won’t resolve to anything. I can’t use this.clicked, since this will be referring to the clicked polygon, not the object drawPolygon and clicked belong to.

Some of you might be thinking “well, have you tried copying clicked into drawPolygon‘s scope with something like var clicked = this.clicked;“? Well done, sir. You’ve just won some internets for your trouble.

Unfortunately, that doesn’t work in this case. It fixes the scope with clicked in the anonymous function. But the problem is that this is used in the clicked method, and clicked‘s this will reference the wrong object. FAIL.

I wonder: What Would Doug Do?

Written by Barry

October 24, 2007 at 6:25 pm

Posted in Nerdy

My God!

with one comment

NASCAR Ladies Pump:

Well, it’s still better than fucking ugg boots and crocs.

Written by Barry

October 19, 2007 at 7:30 pm

Posted in General

JavaScript Late Binding?

with one comment

This is my first nerdy post. It is about JavaScript.

Some of you stopped reading at “JavaScript” yes? Good.

Lets say I have a side bar with an edge that when clicked toggles the side bar. I want to represent this side bar as an object with the methods of show and hide. So I might have code that looks like this:

Sidebar = new function() {
    var edge = $('#sidebar_edge');
    this.show = function() {
        /* do some stuff to the UI blah blah blah I hate IE >.< */
        edge.unbind('click');
        edge.click(this.hide);
    }
    this.hide = function() {
        /* la-di-da! la-di-da! */
        edge.unbind('click');
        edge.click(this.show);
    }
}();


(The code assumes that you are using jQuery, because I am and you should.)

Now, that doesn’t work. When I bind this.hide to the click event on the last line of this.show, this.hide has not been defined. But if I change this.hide to Sidebar.hide, then it’d work just fine.

If any JavaScript gurus are reading this, please explain why that is (without telling me to read the ECMAScript specification :-P).

EDIT: Meh. I just realized that it’s probably a scope thing.

Written by Barry

October 10, 2007 at 12:40 pm

Posted in Nerdy