Posted By

iTony on 03/22/10


Tagged


Versions (?)

Array.prototype.indexOf()


 / Published in: JavaScript
 

URL: http://www.electrictoolbox.com/javascript-indexof/

  1. //This prototype is provided by the Mozilla foundation and
  2. //is distributed under the MIT license.
  3. //http://www.ibiblio.org/pub/Linux/LICENSES/mit.license
  4.  
  5. if (!Array.prototype.indexOf)
  6. {
  7. Array.prototype.indexOf = function(elt /*, from*/)
  8. {
  9. var len = this.length;
  10.  
  11. var from = Number(arguments[1]) || 0;
  12. from = (from < 0)
  13. ? Math.ceil(from)
  14. : Math.floor(from);
  15. if (from < 0)
  16. from += len;
  17.  
  18. for (; from < len; from++)
  19. {
  20. if (from in this &&
  21. this[from] === elt)
  22. return from;
  23. }
  24. return -1;
  25. };
  26. }

Report this snippet  

You need to login to post a comment.