使用 JavaScript 实现类似于 Array.prototype.includes() 方法的自定义函数
问题
我们需要编写一个 JavaScript 函数,该函数位于 Array 的原型对象上。它必须接受一个字面值,如果该值存在于调用该函数的数组中,则返回 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
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP