admin管理员组文章数量:1427361
I was wondering if there is any difference between .filter(':last')
and .last()
?
For me it looks like they're doing the same, but I'm new to jQuery. If there is no difference in the result, which one is remended or is it just a matter of personal preference?
I was wondering if there is any difference between .filter(':last')
and .last()
?
For me it looks like they're doing the same, but I'm new to jQuery. If there is no difference in the result, which one is remended or is it just a matter of personal preference?
Share Improve this question edited Sep 4, 2013 at 20:59 user1228 asked Jun 12, 2013 at 22:05 eLwoodianereLwoodianer 6746 silver badges12 bronze badges 2- 1 They do the same thing, and for most cases you''ll never notice the difference. – adeneo Commented Jun 12, 2013 at 22:07
- 2 The first one operates on all elements, running a non-standard selector against each one. The second one just grabs the last element from the jQuery object, and returns it in a new object. – user2437417 Commented Jun 12, 2013 at 22:14
2 Answers
Reset to default 6last
works by saying "give me the last element from the selection". It takes just two function calls and four lines of code to do so. It can't be done in a quicker way.
filter(':last')
, however, is much more plex. It is a much more flexible system, allowing multiple elements to be returned if that's what you want, or multiple conditions, or a mixture of both. It is much less efficient, because it has to work out what you want. For instance, parsing ':last'
takes a little time, whereas with the last
function it's a simple property lookup.
last
is by far the more efficient.
:last
- Selects the last matched element.
last()
- Reduce the set of matched elements to the final one in the set.
As you can see, they do the same thing (in terms of the end result, anyway).
last()
is slightly faster than :last (although you may not notice it, it's always good to know).
.filter(":last")
, although making the best (performance-wise) out of :last
, still involves more function calls and is still slower than last()
- although it does have its advantages (see @lonesomeday's answer for those).
My remendation however would be to generally use last()
as opposed to the former.
本文标签: javascriptfilter(39last39) vs last()Stack Overflow
版权声明:本文标题:javascript - .filter(':last') vs. .last() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745491272a2660615.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论