Find file
9b7afbf Mar 18, 2013
@shichuan @OCannings
33 lines (27 sloc) 622 Bytes
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: selector cache
Description: using selector cache to avoid requery
*/
// antipattern
$('.list-item').click(function () {
$('.photo').hide();
});
// preferred
var $photo;
// prefix the cache with $ to help identify it as a selector cache later
$('.list-item').click(function () {
$photo = $photo || $('.photo');
$photo.hide();
});
// References
// http://ejohn.org/blog/learning-from-twitter/
</script>
</body>
</html>