Search code examples
javascriptjqueryprototypejsjavascript-frameworkconflict

jQuery and prototype don't work together with Array.prototype.reverse


I use prototype.js and jquery together and I get this error message

Uncaught TypeError: Object [object Object] has no method '_reverse' (Chrome)

I called jQuery.noConflict before

I'm using jQuery.fn.reverse = [].reverse; to have reverse function on jQuery collection


Solution

  • prototype overwrites the native Array.protoype.reverse Function and saves the original in Array.prototype._reverse.

    This snippet takes account of that

    if (typeof []._reverse == 'undefined') {
        $.fn.reverse = Array.prototype.reverse;
    } else {
        $.fn.reverse = Array.prototype._reverse;
    }
    

    old wrong solution

    // for compatibility with prototype.js
    if (![]._reverse) {
      []._reverse = [].reverse;
    }
    jQuery.fn.reverse = [].reverse;