1. // Array.prototype.includes ES2016 ES7
    2. const arr = [1, 2, 3, 4, 5];
    3. console.log(arr.includes(3))//true
    4. console.log(arr.includes(6))//false
    5. //
    6. /**
    7. * 查询数组是否包含某个元素
    8. * 返回值 bool
    9. *
    10. */
    11. /**
    12. * 区分大小写
    13. * 数字字符串与数字
    14. */
    15. const arr = ['1', 'a', '3', '4', '5'];
    16. console.log(arr.includes(1))//false
    17. console.log(arr.includes('A'));//false
    18. // includes 区分大小写
    19. var str = "abcdefg";
    20. console.log(str.includes("C"));//false
    21. // include 第二个参数 fromIndex
    22. const arr = [1, 2, 3, 4, 5];
    23. console.log(arr.includes(3, 5))//false
    24. console.log(arr.includes(5, 3));//true
    25. console.log(arr.includes(2));//true
    26. console.log(arr.includes());//false 直接返回false
    27. console.log(arr.includes.length);// 1
    28. // 负数 arr.length + 负数 [fromIndex,)
    29. console.log(arr.includes(3, -3));//true
    30. // fromIndex >= arr.length return false 不会对数组进行搜索
    31. console.log(arr.include(3, 5));//false
    32. // arr.length + 负数 <-1 整个数组都会搜索 从0开始。
    33. console.log(arr.includes(3, -6));
    34. // 零值相等
    35. var arr = [0, 1, 2, 3, 4, 5, 6];
    36. console.log(arr.includes(0));// true
    37. console.log(arr.includes(+0));// true
    38. console.log(arr.includes(-0));// true
    39. // -----
    40. // 除了字符串和数组,其他类型的数据使用includes
    41. // includes 通用方法 调用着不一定是字符串和数组 ->this 不一定是数组和对象
    42. console.log([].includes.call(true, 'a'));//false
    43. console.log([].includes.call({ 0: 'a' }));//false
    44. console.log([].includes.call({ 0: 'a', length: 1 }, 'a'))//true
    45. Array.prototype.myIncludes = function (value) {
    46. var _this = Object(this),
    47. len = this.length >>> 0,
    48. arg2 = arguments[1],
    49. idx = arg2 < 0 ? Math.max(arg2+len, 0) : Math.min(arg2, len);
    50. if (len === 0) {
    51. return false;
    52. }
    53. while (idx < len) {
    54. if (_this[idx] === value) {
    55. return true;
    56. }
    57. idx++;
    58. }
    59. return false;
    60. }