手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >JavaScript判断数组是否包含指定元素的方法
JavaScript判断数组是否包含指定元素的方法
摘要:本文实例讲述了JavaScript判断数组是否包含指定元素的方法。分享给大家供大家参考。具体如下:这段代码通过prototype定义了数组方...

本文实例讲述了JavaScript判断数组是否包含指定元素的方法。分享给大家供大家参考。具体如下:

这段代码通过prototype定义了数组方法,这样就可以在任意数组调用contains方法

?

1 2 3 4 5 6 7 8 9 10 11 12 /** * Array.prototype.[method name] allows you to define/overwrite an objects method * needle is the item you are searching for * this is a special variable that refers to this instance of an Array. * returns true if needle is in the array, and false otherwise */ Array.prototype.contains = function ( needle ) { for (i in this) { if (this[i] == needle) return true; } return false; }

用法:

?

1 2 3 4 5 // Now you can do things like: var x = Array(); if (x.contains('foo')) { // do something special }

希望本文所述对大家的javascript程序设计有所帮助。

【JavaScript判断数组是否包含指定元素的方法】相关文章:

JavaScript对表格或元素按文本,数字或日期排序的方法

javascript实现可全选、反选及删除表格的方法

JavaScript点击按钮后弹出透明浮动层的方法

JavaScript中的blink()方法的使用

Javascript中prototype属性实现给内置对象添加新的方法

在JavaScript中使用开平方根的sqrt()方法

JavaScript操作XML文件之XML读取方法

JavaScript动态添加style节点的方法

JavaScript中的small()方法使用详解

JavaScript中的异常处理方法介绍

精品推荐
分类导航