Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Tuesday, December 23, 2008

Using ternary to choose between two methods in javascript

I found this little gem while looking at the jQuery source. I'd forgotten that you can reference properties/methods using obj["str"]. Pretty cool tip for getting your code smaller. Plus I like ternaries.

Before:
if($(this).hasClass("myClass")){
 $(this).hide();
}else{
 $(this).show();
}
After:
$(this)[$(this).hasClass("myClass")?"hide":"show"]();

This is doubly useful if you got a big chain in jQuery and don't want to break it for the if statement.

Edit(2009-3-1): As of jQuery 1.3 there's is now an optional parameter for toggle(), toggleClass(), slideToggle(), fadeToggle(). It's a bool that sets the toggle state. So now the previous statement can be written like this as well:

$(this).toggle(!$(this).hasClass("myclass"));

Thursday, December 06, 2007

Rails Steals Brain, Police are Apathetic

I started working with Rails at home and have finally got a chance to try out haml, a ruby templating language that replaces Rhtml. I'm really excited about using it and SASS to create some really clean source.

Plus since I'm learning RoR I'll be able to create sites of much greater complexity on my own then I could being just a front-end developer. I have taken programming classes before so I should be okay.

I cool site that teaches the basics of ruby. It helped me understand some of the peculiarities of ruby. After working with C++ and JavaScript I had a little unlearning to do.