如何使用本机(未覆盖)方法



我的服务没有标准问题。

我在客户端网站的SaaS脚本中使用库Zepto。但2小时前,我的一个客户写信给我,说我的剧本不工作。我检查了所有的代码,发现他用的是Prototype JavaScript framework, version 1.7。在这段代码中我看到:

var Enumerable = (function() {
...
  function each(iterator, context) {...}
  function all(iterator, context) {...}
...    
}();

我的意思是,所有原生数组方法都被覆盖。Zepto.js使用这些本地方法:

each: function(callback){
    emptyArray.every.call(this, function(el, idx){
        return callback.call(el, idx, el) !== false
    })
    return this
},

,

emptyArray = [];

我认为这个版本的原型库有一些错误,因为(从控制台:)

xxx:xxx Uncaught RangeError: Maximum call stack size exceeded

有圆形与Zepto的emtyArray。each和Enumerable。所有功能。

我的问题是,我如何使用方法[].all代替覆盖[].prototype.all ?

更新

var words = ['hello', 'world', '!'];
console.log('Before overwrite');
[].every.call(words, function(el, idx) {
  console.log(el);
});
Array.prototype.every = function(iterator, context)
{
  console.error('not works');
}
console.log('After overwrite');
[].every.call(words, function(el, idx) {
  console.log(el);
});

如何再次使用native .every ?

Iframing在代码片段中是不允许的,但这是工作变体:

Array.prototype.every = function()
{
  console.log('invalid');
}
var iframe = window.document.createElement("iframe");
window.document.documentElement.appendChild(iframe);
var nativeWnd = iframe.contentWindow;
var nativeArray = nativeWnd.Array;
var parent = iframe.parentNode || iframe.parent;
parent.removeChild(iframe);
var oArr = new Array();
var nArr = new nativeArray();
console.log(oArr.every);
console.log(nArr.every);

最新更新