tags:

views:

32

answers:

1

A previous stackoverflow question had this as part of its solution:

$.text([this])

I've never seen that syntax before. The complete statement was:

return $.text([this]).indexOf(myInput) == 0

So I'm thinking $(this).text() couldn't be used.

Q: Where is $.text([this]) mentioned in any of the documentation?

+2  A: 

It's just more efficient to use $.text([this]) when you don't need the jQuery object created by $(this). They do the same thing, just no need to create the jQuery object along the way.

Q: Where is $.text([this]) mentioned in any of the documentation?

It's not AFAIK, you're calling jQuery.text which (in the current version) is just an alias for Sizzle.getText.

Nick Craver
Doesn't the $ in $.text create the jQuery object?
cf_PhillipSenn
@cf_PhillipSenn - Nope, you're calling a static method, where as `$(something)` is actually running `jQuery.fn.init(something)`, creating a new object, then you'd be calling `.text()` on *that*.
Nick Craver
No, $ is more or less a "namespace" in this case. It is also a function (in other cases), which when passed a selector returns a jQuery object.
Domenic