`

处理JS1.6以前的版本不支持indexOf 的方法

 
阅读更多
javascript中indexof方法使用问题小结:
js中的indexOf方法的作用是:
根据给定参数x返回目标字符串(可以看成字符的数组)或数组中与x相等的项的索引。
这个方法相当有用,很多编程语言中都有相对应的实现,javascript中也不例外,然而当我在ie中运行如下代码时候:

却被提示"对象不支持此属性和方法" 。而在chrome,firefox下去运行良好。但google发现原来js中Array的indexOf方法在js1.6版本中才给予了实现,ie7,8中只实现到了js1.3版本,chrome为js1.7版本,ff为js1.8版本。(ie依旧慢半拍)。无奈,只好针对ie进行扩展:


使用如下:


这里我们针对Array的原型进行了扩展,在扩展方法的命名上我加上了"_"字符,我认为这是一个好的习惯,当你在针对原型进行扩展的时候有必要对自己的扩展加以标记。
在_indexOf方法中,我们首先判断当前Array是否实现了"indexOf" 方法,如果是的话直接调用系统发法,否则进行遍历。
分享到:
评论

相关推荐

    JavaScript ES5标准中新增的Array方法

    ES5中新增的不少东西,...indexOf (js v1.6) lastIndexOf (js v1.6) reduce (js v1.8) reduceRight (js v1.8) 1、js中常用的数组Array对象属性: 如图,其中用红色圆圈标记的部分,为ES5新增的属性。 2、浏览器支持

    JavaScript完全自学宝典 源代码

    3.2.html indexOf()函数与lastIndexOf()函数。 3.3.html 截取字符串的子串。 3.4.html 用户自定义的实现slice函数功能的函数。 3.5.html 删除数组最后项的方法。 3.6.html 向数组头添加一个项。...

    判断浏览器的javascript版本的代码

    他老人家说将所要执行的代码放在如< script language=”... ([].indexOf && [].forEach)?JS_ver.push(“1.6”):false; ((function(){try {[a,b] = [0,1];return true;}catch(ex) {return false;}})())?JS_ver.p

    freemarker总结

    旧版本的freemarker采用的是<#comment> 注释 方法 sequences内置方法 sequence?first 返回sequence的第一个值;前提条件sequence不能是null sequence?last 返回sequence最后一个值 sequence?reverse 反转sequence...

    ECMAScript 2018快速入门 高清完整PDF

    5.1.4findIndex方法 41 5.1.5fill方法 41 5.1.6copyWithin方法 41 5.1.7includes方法 41 5.1.8entries方法 42 5.1.9keys方法 42 5.1.10values方法 42 5.2类型化数组 42 5.2.1数据类型 42 5.2.2数组缓冲区 43 5.2.3...

    JavaScript实战

    4.2.3 查找一个字符串:indexOf( )技术 81 4.2.4 使用slice( )提取字符串的一部分 83 4.3 在字符串中查找模式 84 4.3.1 创建和使用基本的正则表达式 84 4.3.2 构建一个正则表达式 85 4.3.3 分组正则表达式的部分 88 ...

    JavaScript权威指南

    JavaScript权威指南 犀牛书 Chapter 1. Introduction to JavaScript Section 1.1. JavaScript Myths Section 1.2. Versions of JavaScript Section 1.3. Client-Side JavaScript Section 1.4. JavaScript ...

    [ASP.NET.AJAX编程参考手册(涵盖ASP.NET.3.5及2.0)].(美)霍斯拉维.扫描版.pdf

    2.1.8 indexOf 2.1.9 insert 2.1.10 parse 2.1.11 remove 2.1.12 removeAt 2.2 ASP.NET AJAX Boolean类型扩展 2.3 ASP.NET AJAX Date类型扩展 2.4 ASP.NET AJAX Object类型扩展 2.5 ASP.NET AJAX String类型扩展 ...

    fruit-machine::slot_machine:老虎机面选择器

    水果机选择器! 随机选择朋友,同事或同学!...const roll_speed = 1.6 ; // Max speed of scrolling (0.5 is slow, 2 is very fast) const roll_duration = 3000 ; // Time until rolling stops in ms 依赖关系=无

    Computer Organization and Design

    , Highlighted glossary terms and definitions appear on the book page, as bold-faced entries in the index, and as a separate and searchable reference on the CD., , A complete index of the material in ...

    BobBuilder_app

    In this version I have done away with the b+tree and hash index in favor of my own MGIndex structure which for all intents and purposes is superior and the performance numbers speak for themselves....

    Python Cookbook英文版

    2.10 Finding the Deep Index of an Item in an Embedded Sequence 2.11 Showing Off Quicksort in Three Lines 2.12 Sorting Objects Using SQL's ORDER BY Syntax 3. Text 3.1 Processing a String One ...

    程序语言设计原理习题解答

    12.9 The Object Model of JavaScript 524 12.10 Implementation of Object-Oriented Constructs 527 Summary • Review Questions • Problem Set •Programming Exercises 530 Chapter 13 Concurrency ...

    ASP.NET程序中常用的三十三种代码.txt

    id="+this.DropDownList1.SelectIndex+"&id1="+...+"’)</script>")  接收参数: string a = Request.QueryString("id"); string b = Request.QueryString("id1");  2.为按钮添加对话框 Button1.Attributes....

    网上购物系统

    j=strTemp.indexOf(NUM.charAt(i));   <p>     if (j==-1) <p>     { <p>     //说明有字符不是数字 <p>     &...

    WordPress Top Plugins.pdf

    Table of Contents Preface 1 Chapter 1: Plugin Basics 5 Safety first 6 Automatic install versus manual install 6 Automatic plugin installation 6 Plugin detail 8 Downloading, unpacking, installing...

    ASP.NET常用代码

    <script language="javascript" for="document" event="onkeydown"> if(event.keyCode==13 && event.srcElement.type!='button' && event.srcElement.type!='submit' && event.srcElement.type!='reset' && event....

    python3.6.5参考手册 chm

    index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python What’s New In Python 3.6 Summary – Release highlights New Features PEP 498: Formatted ...

Global site tag (gtag.js) - Google Analytics