SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding indexOf to Array

    I have a file (etc.js) with some function, one of this is indexOf Array:

    if( typeof Array.prototype.indexOf==='undefined' ) {
    Array.prototype.indexOf = function( v, b, s ) {
    for( var i = +b || 0, l = this.length; i < l; i++ ) {
    if( this[i]===v || s && this[i]==v ) { return i; }
    }
    return -1;
    };
    }

    It is OK with FF. But with IE, when I created an empty array: arreglo[]. I got arreglo as an object with arreglo['indexOf'] = function declaration. I think this is given me a JS error in IE. Can any body help me?

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your prototype doesn't get created in firefox- it has an indexOf Array method already defined. This method works something like indexOf,
    in any browser-

    Code:
    Array.prototype.indexAt=function(wot){
    	var L= this.length;
    	var i= 0;
    	while(i< L){
    		if(this[i]=== wot)return i;
    		++i;
    	}
    	return -1
    }

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Still a problem with IE

    The problem is that with IE, when I created an empty array: arreglo[]. I got arreglo as an object with arreglo['indexOf' or 'indexAt'] = function declaration. I think this is given me a JS error in IE. Can any body help me?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •