Old school textbooks used to have you prepend variable names with special identifiers. For example, “strName”, “intYear”, “bolFemale” would hold things like a string, integer, and boolean.
One trick I like to use when working with jQuery is to prepend jQuery objects with a dollar sign. This not only helps keep your code self-documented, but makes a chain of commands easier to read.
Here’s a bit of code I use on my site:
function toggleCard($card){
$card.toggleClass('expanded')
$card.find('.card')
.toggleClass('c4')
$card.find('.content')
.first()
.toggleClass('hidden')
var scrollTarget = $card.hasClass('expanded') ? $card.find('.content').offset().top - 30 : $card.offset().top
$html.stop(true).animate({
scrollTop: scrollTarget - $nav.outerHeight()
})
}
compared to:
function toggleCard(card){
card.toggleClass('expanded')
card.find('.card')
.toggleClass('c4')
card.find('.content')
.first()
.toggleClass('hidden')
var scrollTarget = card.hasClass('expanded') ? card.find('.content').offset().top - 30 : card.offset().top
html.stop(true).animate({
scrollTop: scrollTarget - nav.outerHeight()
})
}
If you knew my convention, you could tell that both $card and $nav are jQuery selected objects. And $nav isn’t even defined here. Also notice how the jQuery objects are syntax highlighted differently!
Are there any other conventions you use?