当前位置: 代码迷 >> 综合 >> js实现 indexOf()
  详细解决方案

js实现 indexOf()

热度:65   发布时间:2023-12-13 07:38:28.0
if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(selectElement,fromIndex) {
    if (this == null) {
    throw new TypeError('对象是null')}let k,o = Object.create(this);let len = o.length;if (!len) return -1;let n = +fromIndex || 0;if (n >= len) return -1;if (Math.abs(n) == Infinity) {
    n = 0;}k = Math.max(n > 0 ? n : len - Math.abs(n), 0);while(k < len) {
    if (k in o && o[k] === seletElement) return k;k ++;}return -1;}
}
  相关解决方案