JQuery / Javascript синтаксис - PullRequest
       8

JQuery / Javascript синтаксис

4 голосов
/ 29 марта 2012

Я пытаюсь выяснить, что делает приведенный ниже синтаксис. Это код из поповера Bootstraps.

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

Ответы [ 3 ]

6 голосов
/ 29 марта 2012

Давайте разберемся:

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

Это может быть разбито на это:

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

И еще:

var found = $tip.find('.popover-title');

if ($.type(title) == 'object') {
  found['append'](title);
} else {
  found['html'](title);
}

Еще немного:

var found = $tip.find('.popover-title');

if ($.type(title) == 'object') {
  found.append(title);
} else {
  found.html(title);
}
5 голосов
/ 29 марта 2012

Это в основном говорит:

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

Обозначение [...] позволяет динамически выбирать функцию из объекта $tip.find('.popover-title').

2 голосов
/ 29 марта 2012

$("selector")["append"](...) равно $("selector").append(...)

...