Javascript methods that cannot be called from jquery objects?

I read Learning jQuery 1.3 (Jonathan Chaffer and Karl Svedberg), and in the sort table they used .get() before calling .sort() and said

we need to convert jQuery objects to an array of DOM nodes. Although jQuery objects are very similar to arrays in many ways, they do not have available array methods such as .sort ().

the code:

 $("#sort").click(function() { var posts = $("#posts_div .post"); posts.sort(function(a, b) { return ($(a).text()) > ($(b).text()); }); $.each(posts, function(index, post) { $("#posts_div").append(post); }); });​ 

So, I tried to do this without using .get() , but was surprised that it worked even without .get() with the latest jQuery, but did not work with 1.3

So, we made some scripts to clarify

** Doesn't work without .get() jquery 1.2.6 **

Working with .get() jquery 1.2.6

Work without .get() jquery 1.7.2

Working with .get() jquery 1.7.2

So, is it obvious that previously jQuery objects did not use the .sort() function, the same as Javascript arrays? But now they ...

So my question is, what are the functions that jQuery objects do not yet support, so we can keep in mind converting to Javascript arrays before using

+7
source share
2 answers

JQuery objects currently support 3 array methods:

 var methods = 'pop push reverse shift sort splice unshift concat join slice toString indexOf lastIndexOf filter forEach every map some reduce reduceRight'.split(' ') var implemented = $.grep(methods, function(m) { return $.prototype[m] == Array.prototype[m]; }); console.log(implemented); // => ["push", "sort", "splice"] 

They also have slice , but it is not the same slice as arrays:

 $.prototype.slice === Array.prototype.slice // => false 
+4
source

jQuery has a .sort method, it is simply not officially documented because it does not conform to the usual jQuery method formats.

Only methods listed in the api are supported.

.sort is implemented as:

 $.fn.sort = [].sort; 

You can add your own additional array methods as needed in the same way.

 $.fn.reverse = [].reverse; 

If .sort not implemented in your jQuery version, do it yourself.

+1
source

All Articles