I added the following function so I could iterate backwards over some collections:
jQuery.reverseEach = function (object, callback, args) {
var reversed = $(object).get().reverse();
return $(reversed).each(callback, args);
};
Which works great if you call it like this:
$.reverseEach($('#selectedSortItems option:selected'), function() {
$(this).insertAfter($(this).next());
});
However, I wanted to be all slick and make it chainable because I like my syntactic sugar. I added a reference to the original reverseEach function on jQuery’s prototype:
jQuery.prototype.reverseEach = function(callback, args) {
return jQuery.reverseEach(this, callback, args);
};
And now I can call it like this:
$('#selectedSortItems option:selected').reverseEach(function () {
$(this).insertAfter($(this).next());
});
However, I thought that the jQuery.fn.extend function does the same thing in a single step, without the need to reference prototype. I tried to use the jQuery.fn.extend function, like this:
jQuery.fn.extend({
reverseEach: function(object, callback, args) {
var reversed = $(object).get().reverse();
return $(reversed).each(callback, args);
}
});
But whenver I run the chained method that way, I get the following error:
Unable to get value of the property ‘call’: object is null or undefined.
What am I doing wrong with the jQuery.fn.extend method? Am I taking the complete wrong approach, or am I just missing something simple?
Thanks!
You dont need to access the protype you jsut define on
$.fn: