Hello,

Don't know if this is going to be of any use for someone or to enter the
core, but I needed to write a new indexOf that uses an iterator to get the
index from an Array. Array#find finds the value, but I need the index so
here is the code if you want to take a look. I know that maybe Array#indexOf
will be deprecated for native code use, but I find this version  with
iterator quite useful.

(function(){
    Array.prototype._indexOf = Array.prototype.indexOf;
    function indexOf(itemIt, i, context){
        //not an iterator, uses regular indexOf
        if(!Object.isFunction(itemIt)) {
            return this._indexOf(itemIt, i);
        }

        //item as a function is an iterator
        //NaN 'i' is the context, nullifies 'i' to initialize it correctly
after
        if(!Object.isNumber(i)) {
            context = i;
            i = null;
        }

        i || (i = 0);
        var length = this.length;
        if (i < 0) i = length + i;
        for (; i < length; i++)
            if (itemIt.call(context, this[i], i)) {
                return i;
            }
        return -1;
    }
    //redefines indexOf
    Array.prototype.indexOf = indexOf;
})();

I kept the old indexOf for no-iteretor equality cases.

I know that this won't work on a function array if you want the index of a
function inside it. Maybe there should/could be an specific indexOfIt()
method instead of overriding the regular Array#indexOf method. What do you
think?

Cheers,
Rafael

-- 
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to 
prototype-core-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en

Reply via email to