Jquery / javascript syntax

I am trying to understand what the syntax does below. This is the code from Bootstraps popover.

//title is a string $tip.find('.popover-title')[ $.type(title) == 'object' ? 'append' : 'html' ](title) ^ ^ What does this symbol mean? Whats happening here? 
+4
source share
3 answers

Let me break it down:

 $tip.find('.popover-title')[ $.type(title) == 'object' ? 'append' : 'html' ](title) 

It can be divided into:

 var found = $tip.find('.popover-title'); found[$.type(title) == 'object' ? 'append' : 'html'](title); 

And further:

 var found = $tip.find('.popover-title'); if ($.type(title) == 'object') { found['append'](title); } else { found['html'](title); } 

A bit more:

 var found = $tip.find('.popover-title'); if ($.type(title) == 'object') { found.append(title); } else { found.html(title); } 
+6
source

They basically say:

 if(typeof title == 'object') { $tip.find('.popover-title').append(title); } else { $tip.find('.popover-title').html(title); } 

The notation [...] allows you to dynamically select a function from the $tip.find('.popover-title') object.

+5
source

$("selector")["append"](...) is equal to $("selector").append(...)

+2
source

Source: https://habr.com/ru/post/1403994/


All Articles