Use native reduce instead of prototype 1.6.0.3 reduce

I am developing a third-party widget, and my js uses the built-in cut function.

But, when I put the client on the page, it has a prototype 1.6.0.3, and it overrides the reduction function, making something completely unexpected.

How can I use shorthand for my JS instead?

thanks

+4
source share
2 answers

To add a comment to Florent:

<script type="text/javascript">
    Array.prototype.nativeReduce = Array.prototype.reduce;
</script>
<script type="text/javascript" src="/path/to/prototype.js"></script>

Now you can call the native reduce method as follows:

var x = [];

x.nativeReduce(...);

If you cannot control the import order of scripts, then you iframecan do the trick to at least get access to the built-in function again reduce.

(function (document) {
    var iframe = document.createElement("iframe");

    iframe.src = "about:blank";
    iframe.style.display = "none";
    document.documentElement.appendChild(iframe);

    var reduce = iframe.contentWindow.Array.prototype.reduce;

    // Rest of your script goes here

    var arr = [1, 2, 3];
    reduce.call(arr, function() { ... });
})(this.document);
+1

All Articles