我们需要编写一个 JavaScript 函数,它存在于Array.Itmust 接受一个文字值的原型对象上,如果该值存在于它被调用的数组中,则返回 true,否则返回 false。
以下是代码 -
const arr = [1, 2, 3, 4, 5, 6, 7, 8]; const num = 6; Array.prototype.customIncludes = function(num){ for(let i = 0; i < this.length; i++){ const el = this[i]; if(num === el){ return true; }; }; return false; }; console.log(arr.customIncludes(num));输出结果
true