JavaScript square bracket function call

Viewed 4658

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']();
5 Answers
Related