// Array.prototype.includes ES2016 ES7
const arr = [1, 2, 3, 4, 5];
console.log(arr.includes(3))//true
console.log(arr.includes(6))//false
//
/**
* 查询数组是否包含某个元素
* 返回值 bool
*
*/
/**
* 区分大小写
* 数字字符串与数字
*/
const arr = ['1', 'a', '3', '4', '5'];
console.log(arr.includes(1))//false
console.log(arr.includes('A'));//false
// includes 区分大小写
var str = "abcdefg";
console.log(str.includes("C"));//false
// include 第二个参数 fromIndex
const arr = [1, 2, 3, 4, 5];
console.log(arr.includes(3, 5))//false
console.log(arr.includes(5, 3));//true
console.log(arr.includes(2));//true
console.log(arr.includes());//false 直接返回false
console.log(arr.includes.length);// 1
// 负数 arr.length + 负数 [fromIndex,)
console.log(arr.includes(3, -3));//true
// fromIndex >= arr.length return false 不会对数组进行搜索
console.log(arr.include(3, 5));//false
// arr.length + 负数 <-1 整个数组都会搜索 从0开始。
console.log(arr.includes(3, -6));
// 零值相等
var arr = [0, 1, 2, 3, 4, 5, 6];
console.log(arr.includes(0));// true
console.log(arr.includes(+0));// true
console.log(arr.includes(-0));// true
// -----
// 除了字符串和数组,其他类型的数据使用includes
// includes 通用方法 调用着不一定是字符串和数组 ->this 不一定是数组和对象
console.log([].includes.call(true, 'a'));//false
console.log([].includes.call({ 0: 'a' }));//false
console.log([].includes.call({ 0: 'a', length: 1 }, 'a'))//true
Array.prototype.myIncludes = function (value) {
var _this = Object(this),
len = this.length >>> 0,
arg2 = arguments[1],
idx = arg2 < 0 ? Math.max(arg2+len, 0) : Math.min(arg2, len);
if (len === 0) {
return false;
}
while (idx < len) {
if (_this[idx] === value) {
return true;
}
idx++;
}
return false;
}