Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用本机reduce代替原型1.6.0.3 reduce_Javascript_Prototypejs - Fatal编程技术网

Javascript 使用本机reduce代替原型1.6.0.3 reduce

Javascript 使用本机reduce代替原型1.6.0.3 reduce,javascript,prototypejs,Javascript,Prototypejs,我正在开发一个第三方小部件,我的js使用本机reduce函数 但是,当我进入customer页面时,它有prototype 1.6.0.3库,它覆盖了reduce函数,做了一些完全出乎意料的事情 如何让我的JS改用本机reduce 感谢详述弗洛伦特的评论: <script type="text/javascript"> Array.prototype.nativeReduce = Array.prototype.reduce; </script> <scri

我正在开发一个第三方小部件,我的js使用本机reduce函数

但是,当我进入customer页面时,它有prototype 1.6.0.3库,它覆盖了reduce函数,做了一些完全出乎意料的事情

如何让我的JS改用本机reduce


感谢

详述弗洛伦特的评论:

<script type="text/javascript">
    Array.prototype.nativeReduce = Array.prototype.reduce;
</script>
<script type="text/javascript" src="/path/to/prototype.js"></script>
如果无法控制脚本的导入顺序,则
iframe
可能至少可以再次访问本机
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);

我找到的解决方案是使用中实现的.reduce函数


并将其重命名为ReduceText,以避免弄乱所有者页面中的现有LIB。

存储
数组。prototype。在加载prototype之前,在变量中reduce
,然后重置
reduce
。问题是,作为第三方JS,我无法控制客户端将我的脚本放在何处,prototype.js之上或之下您可以请求将PrototypeJS更新为1.7.1吗?该版本会在覆盖它之前检查原生reduce是否存在,并修复许多其他bug。不会,因为这会影响客户网站的其他部分,他不想冒这个风险。是的,Prototype.js自己实现的最奇怪的事情是它做了一件绝对愚蠢的事情:reduce:function(){return this.length>1?this:this[0];}WTF?问题是,作为第三方JS,我无法控制客户端将脚本放置在原型之上或之下的位置。js@Cleyton当前位置我只晚了5年才回复你的评论,但我确实找到了解决办法并更新了我的答案。
(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);