1

这与性能有关,我想知道嵌套的 if-else 语句是否比 jQuesy 选择器查询执行得更快,还是相反?

if (valid)
{
    // do something
}
else if ($(something).parent().data('somePlugin').options.data1 > 0)
{
    error = 'Must be larger than ' + $(something).parent().data('somePlugin').options.data1;
}
else if ($(something).parent().data('somePlugin').options.data2 < 0)
{
    error = 'Must be larger than ' + $(something).parent().data('somePlugin').options.data2;
}

VS

if (valid)
{
    // do something
}
else 
{
    $plugin = $(something).parent().data('somePlugin');

    if ($plugin.options.data1 > 0)
    {
        error = 'Must be larger than ' + $plugin.options.data1;
    }
    else if ($plugin.options.data2 < 0)
    {
        error = 'Must be larger than ' + $plugin.options.data2;
    }
}
4

0 回答 0