SBN & DRY - Less code length in JavaScript
Time is money or something else. So let's start to spare some. SBN, short for square bracket notation, gives you no increase in performance but reduces the length of your code. Furtheron you can follow the DRY principle and reduce some keystrokes.
Example:
cond ? obj.foo("p1","p2","p3") : obj.bar("p1","p2","p3");
vs.
obj[ cond ? "foo" : "bar" ]("p1","p2","p3");
So let's put this into a small jQuery code snippet that shows or hides an element based on a condition. For instance a textfield contains an specific value...
$(this)[ $("#textbox").val()=="foo bar" ? "show" : "hide" ]();
So, hope that helps and is a good start posting to the coderwall.
Written by Henning
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Jquery
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#