javascriptprototypejszepto

How to use native (not overwrote) method


I have not standard problem with my service.

I'm using library Zepto in my SaaS script on client's sites. But 2 hours ago one of my clients wrote me, that my script is not working. I checked all code and found that: he using Prototype JavaScript framework, version 1.7. And in this code I see:

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

I mean, that all native array methods are overwrote. Zepto.js using this native methods:

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

where

emptyArray = [];

I think this version of prototype library has some bug because (from console:)

xxx:xxx Uncaught RangeError: Maximum call stack size exceeded

There are circle with Zepto's emtyArray.each and Enumerable.all functions.

My question is, how can I use method [].all instead overwrited [].prototype.all?

Update

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);
});

How to use native .every again?


Solution

  • Iframing is not allowed in code snippet, but that's is working variant:

    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);