Jquery / javascript: jQuery.fn.reverse = [] .reverse;

This little jQuery plugin:

jQuery.fn.reverse = [].reverse;

How it works? Where is the object binding the prototype of the array for the inverse function? I really don't understand how this works behind the scenes. Some explanations would be nice. Greetings

+5
source share
2 answers

jQuery.fnis an alias for jQuery.prototype.

Thus, this plugin adds the Array function reverseto all objects created using new JQuery(), which is suitable for collections built using $.

And this works because the function reverse makes it applicable to any object that has length and indexed properties. You can test it using this:

var a = {0:'a', 1:'b'};
a.length = 2;
console.log([].reverse.call(a)); // it works
+5

[].reverse .reverse() Array. jQuery .

[] .reverse.

, jQuery $.reverse(), jQuery .

+13

All Articles