Skip to content

Instantly share code, notes, and snippets.

@jackfranklin
Created June 20, 2012 10:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackfranklin/2959277 to your computer and use it in GitHub Desktop.
Save jackfranklin/2959277 to your computer and use it in GitHub Desktop.
//so before you had:
if(options.slide == 'up') { $(this).slideUp(time, options.callback);} // slide Up
else if(options.slide == 'down') { $(this).slideDown(time, options.callback);} // slide down
else if(options.slide == 'toggle') {$(this).slideToggle(time, options.callback);} // slide toggle
/**there's a pattern here
* if options.slide = up, call slideUp
* if it's down, call slideDown
* it it's toggle, call slideToggle
* so you call the method slide + option, with first letter in uppercase
* which is all this code does:
*/
obj["slide" + options.slide.charAt(0).toUpperCase() + options.slide.slice(1)](time, options.callback);
//options.slide.charAt(0) == first character, put it in uppercase
//options.slide.slice(1) == every character except the first one
/**
* in JavaScript you can call methods on an object through two notations, the dot notation: foo.bar or the square brackets one, foo[bar].
* so when I'm calling obj[slide+...] I'm calling obj.slide... but using a different notation
*/
// this bit
if(options.slide in {up:1, down: 1, toggle: 1})
//is a quicker way to do this long statement:
if(options.slide == "up" || options.slide == "down" || options.slide == "toggle")
//because JS's in operator will check to see if a string is a key in an object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment