understanding ternary operator in jquery `show` and `hide` method.

Alexander Solonik

Was just looking at a dropdown fiddle online HERE , there is the below line in the dropdown, like so:

 $(this).closest('.menu')[s.indexOf(g) !== -1 ? 'show' : 'hide']();

now i do understand ternary operators , and also i understand that

 $(this).closest('.menu')[0];

is a syntax often used to derive the 1st element , but i am not quite understanding the above syntax , how does the line get executed , or is it just:

 $(this).closest('.menu').hide();

depending on the result of the ternary operator ?

zamuka

Its very simple. In JS you can access object members not only with object.field_name but also with object['field_name']

So your line of code can be translated as :

if(s.indexOf(g) !== -1) {
   // $(this).closest('.menu').show()
   $(this).closest('.menu')['show']() 
} else {
   // $(this).closest('.menu').hide()
   $(this).closest('.menu')['hide']() 
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related