最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript判斷數(shù)組是否包含指定元素的方法

 更新時(shí)間:2015年07月01日 12:14:40   作者:不吃皮蛋  
這篇文章主要介紹了JavaScript判斷數(shù)組是否包含指定元素的方法,涉及javascript中contains方法的使用技巧,需要的朋友可以參考下

本文實(shí)例講述了JavaScript判斷數(shù)組是否包含指定元素的方法。分享給大家供大家參考。具體如下:

這段代碼通過(guò)prototype定義了數(shù)組方法,這樣就可以在任意數(shù)組調(diào)用contains方法

/**
 * 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;
}

用法:

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

希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

扎囊县| 漠河县| 凉山| 盐城市| 扶余县| 克山县| 农安县| 怀安县| 武隆县| 惠东县| 桃江县| 华蓥市| 平定县| 秦安县| 山西省| 资中县| 惠东县| 宁南县| 疏勒县| 漳浦县| 崇明县| 聂拉木县| 凤庆县| 金寨县| 洪湖市| 元氏县| 双牌县| 仁化县| 巴马| 尤溪县| 府谷县| 普定县| 兴安盟| 沾益县| 新昌县| 长阳| 宁远县| 平顺县| 泗阳县| 刚察县| 二连浩特市|