Created
October 31, 2011 16:20
-
-
Save zhuangya/1327877 to your computer and use it in GitHub Desktop.
indexOf prototype for IE 9-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Refs: | |
* http://nootn.com/blog/Develop/35/ | |
* http://soledadpenades.com/2007/05/17/arrayindexof-in-internet-explorer/#comment-57199 | |
* http://ecma262-5.com/ELS5_HTML.htm#Section_15.4.4.14 | |
*/ | |
if (!Array.prototype.indexOf) { | |
Array.prototype.indexOf = function(el, index) { | |
var n = this.length >>> 0, i = ~~index; | |
if (i < 0 ) i += n; | |
for (; i < n; i++) if (i in this && this[i] === el) return i; | |
return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
这是扩展 Array 的。.indexOf 执行的 context 不是 Array,那么应该让他自然报错啊,看一下 [].length 永远都是大于或者等于0。