Was browsing the jQuery source code when I met this line:
jQuery(this)[ state ? "show" : "hide" ]();
Are there any advantages over
state ? jQuery(this).show() : jQuery(this).hide();
?
Standalone example:
var object = {
foo: function() {
alert('foo');
},
bar: function() {
alert('bar');
}
};
object[true ? 'foo' : 'bar']();
object[false ? 'foo' : 'bar']();
There’s no advantage in performance. But there’s an advantage in length of code (if you see it as an advantage), and DRY principle (don’t repeat code) specially if you have many parameters in your functions.
Consider the following:
Versus:
As you can see, you repeat ‘a lot’ of code in the second way
Hope this helps. Cheers