سؤال

I'm implementing the .every function on an array. I'm in an old javascript engine environment and the function below didn't make the compiler too happy (Rhino). The version of javascript is ECMA 262 standard JavaScript 1.5.

However, the code Mozilla supplies to make .every backwards compatible doesn't seem to work in the engine so I'm trying to implement an easier, less functional version. Thus I want to understand how the script works in order to make the required adjustments.

My question; how could I implement an extremely non-complex version of the below that is guaranteed to work with JS 1.5?

if (!Array.prototype.every)
{
    Array.prototype.every = function(fun /*, thisp */)
    {
        "use strict";

         if (this == null)
             throw new TypeError();

         var t = Object(this);
         var len = t.length >>> 0;
         if (typeof fun != "function")
             throw new TypeError();

         var thisp = arguments[1];
         for (var i = 0; i < len; i++)
         {
             if (i in t && !fun.call(thisp, t[i], i, t))
             return false;
         }

         return true;
    };
};
هل كانت مفيدة؟

المحلول

The function works with JS 1.5. I had other issues that broke it, when I fixed those .every worked like it should.

A bit embarrassing of course, but I did some good research that helped me in other ways thank to this question!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top