Array对象的方法实现(5)----Array.prototype.includes(实现常规参数的功能)
来源:互联网 发布:java iterator重置 编辑:程序博客网 时间:2024/05/17 04:54
10,Array的includes方法
includes() 方法用来判断一个数组是否包含一个指定的值,如果是,酌情返回 true或 false。
语法:arr.includes(searchElement) 或 arr.includes(searchElement, fromIndex)
注意:1,返回值为true(找到指定值),false(未找到指定值)。2,不改变原数组
Array.prototype._includes = function(searchElement,fromIndex){if (this === null) { throw new TypeError('"this" is null or not defined'); }let that = Object(this),len = that.length >>> 0,param = arguments,index = fromIndex | 0;if(len === 0){return false;}startIndex = Math.max(index >= 0 ? index : len - Math.abs(index), 0);while(startIndex < len){if(String(that[startIndex]) === String(param[0]))return true;startIndex++}return false;}
注意:
(1,通过startIndex获取开始查找的位置,如果开始位置大于length,返回false
(2,将that[startIndex]和param[0]转化为字符串比较的原因是我发现官方给的
console.log([1, 2, NaN].includes(NaN)); // true我们都知道NaN === NaN会返回false,也就是说如果我们直接进行比较,会发现[1, 2, NaN]._includes(NaN)返回的是false,所以我在此处做了一个字符串转换处理。
测试:
var arr = ['a', 'b', 'c'];console.log(arr.includes('a', -100)); // trueconsole.log(arr.includes('b', -100)); // trueconsole.log(arr.includes('c', -100)); // trueconsole.log(arr._includes('a', -100)); // trueconsole.log(arr._includes('b', -100)); // trueconsole.log(arr._includes('c', -100)); // trueconsole.log(arr.includes('c', 3)); //falseconsole.log(arr.includes('c', 100)); // falseconsole.log(arr._includes('c', 3)); //falseconsole.log(arr._includes('c', 100)); // falseconsole.log('_includes:'+[1, 2, 3]._includes(2)); // trueconsole.log([1, 2, 3]._includes(4)); // falseconsole.log([1, 2, 3]._includes(3, 3)); // falseconsole.log([1, 2, 3]._includes(3, -1)); // trueconsole.log('_includes:'+[1, 2, NaN]._includes(NaN)); // trueconsole.log('_includes:'+[1, 2, +0]._includes(-0));//trueconsole.log('_includes:'+[1, 2, -0]._includes(+0));//true
这样修改后示例测试基本都是对的,请问大神这个位置的NaN进行比较时,返回的是true,浏览器是怎么处理这个问题?
阅读全文
0 0
- Array对象的方法实现(5)----Array.prototype.includes(实现常规参数的功能)
- Array对象的方法实现(6)----Array.prototype.indexOf(实现常规参数的功能)
- Array对象的方法实现(1)----Array.prototype.push和Array.prototype.concat(实现常规参数的功能)
- Array对象的方法实现(2)----Array.prototype.every和Array.prototype.fill(实现常规参数的功能)
- Array对象的方法实现(3)----Array.prototype.filter和Array.prototype.find(实现常规参数的功能)
- Array对象的方法实现(4)----Array.prototype.findIndex和Array.prototype.forEach(实现常规参数的功能)
- Array.prototype.filter()的实现
- 使用Array.prototype.slice实现带参数的setTimeout
- map方法学习,封装函数--实现Array.prototype.map()的功能
- 浅谈Array.prototype.map(func)方法的实现
- JavaScript Array对象indexOf()方法的实现
- ES2016系列: Array.prototype.includes
- 【JavaScript】(6)Object、String、Array、Date对象的方法、属性(constructor、prototype)
- 重写javascript的Array.prototype.push方法实现类似java.util.set添加不重复元素
- javascript 中Array 常规方法实现( Remove,insertAt,clear,clone)
- php array的实现
- js模拟实现Array的sort方法
- ECMAScript5新增Array方法forEach的实现
- Toolbar、AppBarLayout、CollapsingToolbarLayout分步解析
- Quartz的Misfire处理规则 错过任务执行时间的处理机制
- 开发动态资源--动态网站
- 第四节:元素定位方法
- 数值优化(Numerical Optimization)学习系列(一)-概述
- Array对象的方法实现(5)----Array.prototype.includes(实现常规参数的功能)
- JVM调优之jstack找出最耗cpu的线程并定位代码
- Git-别名(笔记)
- 进程跟线程的举例说明
- Java开发MongoClient邮件服务自动化
- 解决办法:The name 'Response' does not exist in the current context
- caffe2 学习笔记04-训练网络并进行预测
- python之获取路径
- maven新建web工程